0

Just started learning ASP.NET, did some practice with TextBox and Button - getting sum of two numbers.

I want the TextBoxes Not to retain their values after clicking the button, by setting the EnableViewState = "false". This doesn't work and by searching posts, I got some explanations on this issue...The problem is, with all the explanations, I can't manage to find a solution.

There are some possible ways I found by searching, to make the TextBox not retain its value:

  1. setting the TextBox to "": TextBox1.Text ="" source: http://aspadvice.com/blogs/joteke/archive/2004/03/15/2273.aspx didn't work for me..

  2. "...mark the text box read-only or marked its visibility false..." source: EnableViewState property in textbox didn't work too...

My code:

<asp:TextBox ID="txtFirstNumber" runat="server" EnableViewState="false"></asp:TextBox>
<asp:TextBox ID="txtSecondNumber" runat="server" EnableViewState="false"></asp:TextBox>
<asp:TextBox ID="txtSum" runat="server" Enabled="False"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add"onclick="btnAdd_Click1" />

code-behind page

protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnAdd_Click1(object sender, EventArgs e)
{
    int a = Convert.ToInt32(txtFirstNumber.Text);
    int b = Convert.ToInt32(txtSecondNumber.Text);
    int c = a + b;

    txtSum.Text = c.ToString();
}
4

1 回答 1

1

要清除文本框,请将其Text属性设置为nullor ""

protected void btnAdd_Click1(object sender, EventArgs e)
{
    int a = Convert.ToInt32(txtFirstNumber.Text);
    int b = Convert.ToInt32(txtSecondNumber.Text);
    int c = a + b;

    txtSum.Text = c.ToString();
    txtFirstNumber.Text = "";
    txtSecondNumber.Text = "";
}
于 2013-09-08T21:31:59.127 回答