Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I wish to update the Session value, but it's not working ....

From the below code, while Button2_Click() its giving the old session value.. "paramu"
Not giving the"kodi Thatha"

Thanks for the guidances...

My Codes
C#
protected void Page_Load(object sender, EventArgs e)
{
    Session["Field1"] = "paramu";
}

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = Session["Field1"].ToString();
    Session["Field1"] = "Kodi Thatha";
}

protected void Button2_Click(object sender, EventArgs e)
{
    TextBox1.Text = Session["Field1"].ToString();
}
Posted
Updated 21-Aug-13 21:00pm
v2

You can add this

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)//add this line
        Session["Field1"] = "paramu";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = Session["Field1"].ToString();
        Session["Field1"] = "Kodi Thatha";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = Session["Field1"].ToString();
    }
 
Share this answer
 
Comments
Thanks7872 22-Aug-13 3:24am    
↑voted. Please remove the duplicate one. I know why it worked this way. :)
Your fault. If you click Button1 then only it'll change session value to "Kodi Thatha".
 
Share this answer
 
Comments
Still, it will again change the Session value to "paramu" after the Button2 click, as it will Post Back. So, OP needs to check IsPostBack property. Check my Answer - Solution 5.

Thanks,
Tadit
As per the ASP.NET Page Life Cycle[^], the page will post back when you fire any event.

As you are clicking on Button2 after Button1 (which changes the Session value), it will then post back the page and fire Page_Load again. So, Session again changes to "paramu".
C#
protected void Page_Load(object sender, EventArgs e)
{
    Session["Field1"] = "paramu";
}

So, if you want to restrict that, then use Page.IsPostBack Property[^] like below...
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["Field1"] = "paramu";
    }
}
 
Share this answer
 
That's because the system doesn't work as you think.
Your page is not preserved in Server memory while the user works out what to do: when the user presses a button, the Page_Load event is raised, and the handler runs. The page is set up, including doing your Session value setting, and then the Click event is honored. So when he clicks the Button1, the Page_Load sets the Session to "paramu" then the Click sets it to "Kodi Thatha".
When he clicks the Button2, the Page_Load sets the Session back to "paramu" then the Button2 Click handler reads it back.

To avoid this, try this:
C#
protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack) 
        {
        Session["Field1"] = "paramu"; 
        }
    }
 
Share this answer
 
use
C#
protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
        {
        Session["Field1"] = "paramu";
        }
    }

this will must help,in your case value was overriding again and again.
 
Share this answer
 

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