0

1源页面具有如下页面加载方法:

protected void Page_Load(object sender, EventArgs e)
        {
                TextBox1.Text = DateTime.Today.AddDays(1).ToShortDateString();
        }

它将产生一个 textbox1.text 以显示明天源页面呈现的日期。我将此源页面交叉回发到目标页面,并且在目标页面加载事件中我有

if (Page.PreviousPage != null && PreviousPage.IsCrossPagePostBack == true)
            {
              TextBox SourceTextBox1 = (TextBox)Page.PreviousPage.FindControl("TextBox1");
                if (SourceTextBox1 != null)
                {
                    Label1.Text = SourceTextBox1.Text;
                 }
              }

问题是如果用户更改 textbox1 的内容,假设目标页面上的 label1 应该捕获用户输入并显示它,但现在它只显示我在源页面加载事件中设置的任何内容。我了解自我页面回发生命周期,但这是跨页面回发。IMO,源页面加载事件与此无关,但为什么它会覆盖用户输入?任何想法。

4

1 回答 1

1

Just surround this with a if(!IsPostBack) check:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        TextBox1.Text = DateTime.Today.AddDays(1).ToShortDateString();
    }
}

Otherwise the value will be overwritten on every postback. So when you Server.Transfer it to the other page it is already changed.

于 2013-03-12T15:42:22.643 回答