Asp.net 会话[“”]导致某些变量内容丢失

Asp.net 会话[“”]导致某些变量内容丢失,asp.net,session,Asp.net,Session,我在ASP.NET站点中使用委托。 我使用会话[]保存此委托的值。 当调用它时,它调用正确的方法,但在被调用的方法体中,所有变量都具有来自先前状态的值。 当我不使用委托直接调用该方法时,没有问题 我已经编写并测试了这个虚拟代码,它以更全面的方式说明了问题: namespace WebApplication_test_update_controls { public partial class _Default : System.Web.UI.Page { publ

我在ASP.NET站点中使用委托。 我使用会话[]保存此委托的值。 当调用它时,它调用正确的方法,但在被调用的方法体中,所有变量都具有来自先前状态的值。 当我不使用委托直接调用该方法时,没有问题

我已经编写并测试了这个虚拟代码,它以更全面的方式说明了问题:

namespace WebApplication_test_update_controls
{
    public partial class _Default : System.Web.UI.Page
    {
        public delegate void My_delegate();

        public string str;
        public int age;
        My_delegate del1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) //first load
            {
                str = "postback value";
                age = 1;
                del1 = LB_Add_Text;
                TextBox1.Text = "fennec postback";

                Session["str"] = str;
                Session["age"] = age;     
                Session["del1"] = del1;
            }

            if(IsPostBack)
            {
                del1 = (My_delegate)Session["del1"];
                str = (string) Session["str"];
                age = (int) Session["age"];

            }
        }

protected void Button5_Click(object sender, EventArgs e)
        {
            str = "Value Button event";
            age = 10;
            TextBox1.Text = "fennec Button event";



            //Call of the method without using the delegate, LB_Add_Text is executed with correct values
            LB_Add_Text();

           //!!!Call of the method using the delegate, LB_Add_Text is executed with INCORRECT values!!!
            del1();

             //I explicitally point del1 to LB_Add_Text again (although it seems to already point there looking at debugger
             del1 = LB_Add_Text;

            //then B_Add_Text is executed with correct values this time
            del1();


        }


        public void LB_Add_Text()
        {
            ListBox3.Items.Add(TextBox1.Text);
        }

 }
}
正如你在评论中看到的, 所有值在第一次页面加载后存储在会话中,并在回发年龄、str和委托del1后检索

当我点击一个按钮时,我将新的值设置为age和str。调试器显示del1仍然指向LB_Add_文本,因此在回发后显然正确地从会话[del1]中检索

我直接调用LB_Add_Text:一切正常,它使用预期值:

   str = "Value Button event";
   age = 10;
   TextBox1.Text = "fennec Button event";
然后,我调用了应该执行相同操作的委托:它不使用预期值。它正确地调用LB_Add_Text,但使用从会话检索到的值:

str = "postback value";
                age = 1;
                del1 = LB_Add_Text;
                TextBox1.Text = "fennec postback";
最后,我明确地将del1重新指向LB_Add_Text

del1 = LB_Add_Text;
,然后我再次调用它:然后一切按预期工作,并使用预期的变量值调用LB_Add_Text:

str = "Value Button event";
   age = 10;
   TextBox1.Text = "fennec Button event";
我觉得整个问题来自:

del1 = (My_delegate)Session["del1"];
这在某种程度上并不像我期望的那样,并且不仅存储/检索委托签名

你能告诉我我错过了什么吗


Thx提前。

更新值时,您没有更新会话值。我认为这不是主要问题,即使我更新了,问题仍然存在。此外,请注意,控件的文本在回发TextBox1.text后不应丢失其内容也会出现问题。这里有人有建议吗?没有人对此有想法吗?