如何更新会话值? [英] How to update the Session value?

查看:70
本文介绍了如何更新会话值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望更新会话值,但它不起作用....



从下面的代码中,当Button2_Click()给它以前会话价值..paramu

不给kodi Thatha



感谢您的指导...



我的代码

  protected   void  Page_Load( object  sender,EventArgs e)
{
Session [ Field1] = paramu;
}

受保护 void Button1_Click( object sender,EventArgs e)
{
TextBox1.Text = Session [ 字段1]的ToString();
会话[ Field1] = Kodi Thatha;
}

受保护 void Button2_Click( object sender,EventArgs e)
{
TextBox1.Text = Session [ 字段1]的ToString();
}

解决方案

你可以添加这个



< pre lang =cs> protected void Page_Load( object sender,EventArgs e)
{
if (!Page.IsPostBack) / / 添加此行
会话[ Field1] = paramu;
}

受保护 void Button1_Click( object sender,EventArgs e)
{
TextBox1.Text = Session [ 字段1]的ToString();
会话[ Field1] = Kodi Thatha;
}

受保护 void Button2_Click( object sender,EventArgs e)
{
TextBox1.Text = Session [ 字段1]的ToString();
}


你的错。如果单击 Button1 ,则只会将会话值更改为Kodi Thatha。


根据ASP.NET页面生命周期 [ ^ ],当你发射任何事件时,页面都会回发。



当您在 Button1 (更改 Session 值)之后单击 Button2 时,它将回发再次点击并点击 Page_Load 。因此,Session再次更改为 paramu

  protected   void  Page_Load( object  sender,EventArgs e)
{
Session [ Field1] = paramu;
}



所以,如果你想限制它,那么使用 Page.IsPostBack Property [ ^ ]如下所示...

  protected   void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
{
Session [ Field1] = paramu;
}
}


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

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();
}

解决方案

You can add this

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();
    }


Your fault. If you click Button1 then only it'll change session value to "Kodi Thatha".


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".

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...

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["Field1"] = "paramu";
    }
}


这篇关于如何更新会话值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆