Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
3.77/5 (3 votes)
I need a bit help. My problem is when assign value in session can't find a proper value another page. my code snippet is
JavaScript
var _user = document.getElementById("username").value;
'<%Session["UserName"] = "'+ _user +' "; %>'


when we are get a value another page i find value '+_user+'. How can find enter values and code behind. My code behind
C#
string my = Session["UserName"].ToString();

any buddy help me..
Posted
v2
Comments
♥…ЯҠ…♥ 10-Dec-13 2:42am    
what value do you get in _user?
joginder-banger 10-Dec-13 2:47am    
User Name TextBox.
Please check my answer.
Hi joginder,

In case you have unknowingly rejected my answer, please accept it again.
Else if you need any information about the answer, feel free to hit reply.

Thanks,
Tadit
joginder-banger 10-Dec-13 5:21am    
I can understand your code sir.

You can't set Session value directly in this way by JavaScript because Sessions are managed at Server side.

Instead of setting Session at Client Side, you can call a WebMethod and set the Session inside it.
  1. Define one WebMethod in Code Behind like...
    C#
    [System.Web.Services.WebMethod]
    public static void SetUserName(string userName)
    {
        Page objPage = new Page();
        objPage.Session["UserName"] = userName;
    }
  2. To call this method from aspx page, include a ScriptManager to enable Pagemethods.
    <asp:ScriptManager EnablePageMethods="true" ID="ScriptManager" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>

    And write the following code...
    JavaScript
    var _user = document.getElementById("username").value;
    PageMethods.SetUserName(_user);
 
Share this answer
 
Hi,
Try like this given in the link as check the session value after setting it on the client side.
http://shekharshetemcts.wordpress.com/2013/11/27/how-to-access-session-variables-using-javascript-in-asp-net/[^]
This will ensures you on the client side that weather the session is set correctly or not
 
Share this answer
 
Comments
As I said in my answer that Sessions set at client side will never reflect on Server Side if Page Posts back. You can set and get at client side as said in the link you have given.
But that does not serve as a Session because you can't get the value at Server side.

Check my answer. I have suggested to call one WebMethod to set the Session.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900