【问题标题】:confusing Page_Load behavior令人困惑的 Page_Load 行为
【发布时间】:2012-05-31 04:04:54
【问题描述】:

这就是我正在尝试的:

公共部分类_默认:System.Web.UI.Page
{

String test = "hi ";

protected void Page_Load(object sender, EventArgs e)
{


        test = test + test;
        Button1.Value =  test;

}

protected void Button2_Click(object sender, EventArgs e)
{
    Button1.Value = "u're trolled !";
}
protected void Button1_Click(object sender, EventArgs e)
{

}

}

第一次加载页面时,它会显示“hi hi”作为 Button1 的值。但是每当我单击 Button2 时,它都会显示“你被拖了!”作为 Button1 的值。我的问题是,如果每次单击按钮并调用 Page_Load 时页面都会回发,那么为什么它会显示“你被拖了!”而不是附加“嗨”??不是每次页面重新加载时都会调用 Page_Load 吗?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    使用此代码知道什么时候来的值:

    String test = "hi ";
    
     protected void Page_Load(object sender, EventArgs e)
    {
    
    
        test = test + test;
        Button1.Value +=  test;
    
    }
    
    protected void Button2_Click(object sender, EventArgs e)
    {
    Button1.Value += "u're trolled !";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    
    }
    

    【讨论】:

    【解决方案2】:

    您需要了解 ASP.NET 页面生命周期,正如 Code Addict 所说的那样,它是导致您遇到问题的事件序列。

    以下链接对您很有帮助。

    http://msdn.microsoft.com/en-us/library/ms178472.aspx

    【讨论】:

      猜你喜欢
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多