在Javascript中指定textbox的值,并在与C的会话中设置该值#

在Javascript中指定textbox的值,并在与C的会话中设置该值#,javascript,c#,asp.net,session,events,Javascript,C#,Asp.net,Session,Events,我已经为一个Javascript/C问题挣扎了一段时间。我一直在尝试从Javascript设置会话变量。我以前尝试过使用页面方法,但结果导致javascript崩溃 在javascript中: PageMethods.SetSession(id_Txt, onSuccess); 此页面方法: [System.Web.Services.WebMethod(true)] public static string SetSession(string value) { Page aPage =

我已经为一个Javascript/C问题挣扎了一段时间。我一直在尝试从Javascript设置会话变量。我以前尝试过使用页面方法,但结果导致javascript崩溃

在javascript中:

PageMethods.SetSession(id_Txt, onSuccess);
此页面方法:

[System.Web.Services.WebMethod(true)]
public static string SetSession(string value)
{
    Page aPage = new Page();
    aPage.Session["id"] = value; 
    return value;
}
我在这方面没有取得任何成功。因此,我尝试从javascript设置textbox的值,并在我的c#中放置一个ContextChanged事件来设置会话变量,但没有触发该事件

在javascript中:

document.getElementById('spanID').value = id_Txt;
在html中:

<asp:TextBox type="text" id="spanID" AutoPostBack="true" runat="server" 
ClientIDMode="Static" OnTextChanged="spanID_TextChanged"
style="visibility:hidden;"></asp:TextBox>

有人知道为什么我的所有事件都没有被解雇吗?你有我可以尝试的替代方案吗

我发现了这个问题,我没有
enableSession=true
,我不得不使用
HttpContext.Current.Session[“id”]=value
,就像mshsayem所说的那样。现在,我的事件已正确触发,会话变量已设置。

首先,确保已启用sessionState(web.config):

样本aspx:

<head>
    <script type="text/javascript">
        function setSessionValue() {
            PageMethods.SetSession("boss");
        }
    </script>
</head>
<asp:ScriptManager ID="sc1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>

<asp:Button ID="btnSetSession" Text="Set Session Value (js)" runat="server" OnClientClick="setSessionValue();" />
<asp:Button ID="btnGetSession" Text="Get Session Value" runat="server" OnClick="ShowSessionValue" />
<br/>
<asp:Label ID="lblSessionText" runat="server" />

在静态
WebMethod
中,使用
HttpContext.Current.Session[“id”]=value常见的黑客:放置一个隐藏的asp按钮(
display:none
)和一个隐藏字段
OnClientClick单击隐藏按钮的
,设置隐藏字段。在
OnClick
处理程序(cs)中,从隐藏字段读取值。调用js
$(“#buttonId”)。单击()
触发事件。
<sessionState mode="InProc" timeout="10"/>
<asp:ScriptManager ID="sc1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>
HttpContext.Current.Session["my_sessionValue"] = value;
<head>
    <script type="text/javascript">
        function setSessionValue() {
            PageMethods.SetSession("boss");
        }
    </script>
</head>
<asp:ScriptManager ID="sc1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>

<asp:Button ID="btnSetSession" Text="Set Session Value (js)" runat="server" OnClientClick="setSessionValue();" />
<asp:Button ID="btnGetSession" Text="Get Session Value" runat="server" OnClick="ShowSessionValue" />
<br/>
<asp:Label ID="lblSessionText" runat="server" />
[System.Web.Services.WebMethod(true)]
public static string SetSession(string value)
{
    HttpContext.Current.Session["my_sessionValue"] = value;
    return value;
}

protected void ShowSessionValue(object sender, EventArgs e)
{
    lblSessionText.Text = Session["my_sessionValue"] as string;
}