0

我有一个购物车,对于产品交付的时间段选择,我想显示下一周的日期、第一列(每行中的每天)和另一列中的时间段,格式如下:

Column1                            Column2
Aug 19, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM
Aug 20, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM
Aug 21, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM
Aug 22, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM
Aug 23, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM
Aug 24, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM
Aug 25, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM

我尝试使用带有radiobuttonlist(用于时间槽)的gridview,但问题是用户能够选择每天的时间。因此,作为替代方案,我正在尝试检查在 javascript 结束时选择的 TimeSlot,并且因为我希望下一个结帐向导可以访问 Slot,所以我想在会话中存储选定的时间段。如果未选择,我可以验证时隙,如果选择,我可以在警报中获取值。但是,当我尝试将值从隐藏字段保存到会话时(保存在客户端的按钮单击上),我没有将会话值保存到其他页面。下面是我正在尝试的代码:

 <script>
        function SaveToHiddenField() {
            if ($("input:radio:checked").length > 0) {
                $radio = $("input:radio:checked");
                document.getElementById('<%= hdnField.ClientID %>').value = $radio.val();
                <% Session["Slot"] = hdnField.Value; %>
                window.location.href='default2.aspx'
                return true;
            }
            else {
                alert("Nothing Selected");
                return false;
            }
        }
    </script>

<asp:Button ID="btnSession" runat="server" Text="Save to Session" OnClientClick="return SaveToHiddenField()" />
<asp:HiddenField ID="hdnField" runat="server" />

谁能告诉我我在做什么错,或者任何其他可能是我查询的更好选择的替代方案。

4

3 回答 3

1

问题已解决,在按钮上单击我写了以下代码:

Session["Slot"] = hdnField.Value;
        Response.Redirect("default2.aspx");

现在,当页面重定向到 default2.aspx 时,我有了会话值。

谢谢大家,
特别是@JasonP。

于 2013-08-19T18:42:33.593 回答
0

您可以使用 ASP.NET AJAX 页面方法来存储和检索会话值,如下所示:

代码隐藏default1.aspx

[WebMethod(EnableSession = true)]
public static void StoreSessionValue(string theValue)
{
    HttpContext.Current.Session["TheValueToStore"] = theValue;
}

现在在您的标记中default1.aspx,使用 jQuery 调用此页面方法,如下所示:

$.ajax({
    type: "POST",
    url: "defaul1.aspx/StoreSessionValue",
    data: "{'theValue': document.getElementById('<%= hdnField.ClientID %>').value}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        window.location.href='default2.aspx';
    }
});

代码隐藏default2.aspx

[WebMethod(EnableSession = true)]
public static string RetrieveSessionValue()
{
    return HttpContext.Current.Session["TheValueToStore"].ToString();
}

现在在您的标记中default2.aspx,使用 jQuery 调用此页面方法,如下所示:

$.ajax({
    type: "POST",
    url: "defaul1.aspx/RetrieveSessionValue",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        // Do something with the session value that came back in the variable "msg"
    }
});
于 2013-08-19T18:43:39.703 回答
0

在 C# 中

Session["MySes"] = "SessionValue";

在 Javascript 中

 var ScriptSes = "<%= Session["MySes"]%>"; 

在 HTML 中

 <input type="button"     value="Save to Session"  onclick="SaveToHiddenField()" />

在 html 中使用 html 按钮标签,当您使用 asp.net 按钮控件时,它会加载页面,然后您的变量会话值销毁,因此您无法获取会话值。所以使用 html 输入类型按钮而不是 asp.net 按钮控制器

于 2016-08-19T04:09:50.620 回答