textbox.text始终返回空字符串而不是用户输入的文本

git*_*agi 9 asp.net

我很惊讶昨晚看到我的代码工作正常,第二天突然我的textbox.text总是有空字符串..
我的代码是:

 Name of Event* :

    <br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
Run Code Online (Sandbox Code Playgroud)

代码背后:

protected void Page_Load(object sender, EventArgs e) {
} 

protected void create_Click(object sender, EventArgs e) {

    if (!object.Equals(Session["UserLoginId"], null)) { 

        int mid = 0; 
        int cid = 0; 
        bool valid = true;

        if (this.TextBox1.Text == "") {

            error.Text = "<p style='color:red'>Marked Fields are compulsory!!</p>"; 

        }

        else { 
            .... // database insert .... 
        }
Run Code Online (Sandbox Code Playgroud)

我总是以error.text值结束.

为什么?

小智 5

有一个类似的问题,文本框在添加新行时被清除.单击添加按钮时,页面重新加载的问题.

修复了以下问题:

Sub Page_Load
  If Not IsPostBack Then 
    BindGrid()
  End If
End Sub
Run Code Online (Sandbox Code Playgroud)

根据Microsoft的文档http://msdn.microsoft.com/en-us/library/aa478966.aspx.


Any*_*are 1

如果页面回发,则用户输入的所有数据都将被删除,因为控件是无状态的,因此您应该通过 EnableViewState = true 保留数据输入。