I've been facing a problem for some time now related to setting a Session variable from Javascript in a C# environment. I attempted to use page methods previously, but it ended up causing my Javascript code to crash.
Here's the snippet of Javascript code I used:
PageMethods.SetSession(id_Txt, onSuccess);
And here's the corresponding page method in C#:
[System.Web.Services.WebMethod(true)]
public static string SetSession(string value)
{
Page aPage = new Page();
aPage.Session["id"] = value;
return value;
}
Unfortunately, this approach didn't work for me. So, I then tried setting the value of a textbox using Javascript and included an OnTextChanged event in my C# code to set the session variable. But it seems like the event is not triggering.
In the Javascript code:
document.getElementById('spanID').value = id_Txt;
The HTML part looks like this:
<asp:TextBox type="text" id="spanID" AutoPostBack="true" runat="server"
ClientIDMode="Static" OnTextChanged="spanID_TextChanged"
style="visibility:hidden;"></asp:TextBox>
In the C# code behind:
protected void spanID_TextChanged(object sender, EventArgs e)
{
int projectID = Int32.Parse(dropdownProjects.SelectedValue);
Session["id"] = projetID;
}
I'm puzzled as to why none of my events are being triggered. Does anyone have any insights on this issue or perhaps an alternative solution that I could experiment with?