0
votes

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();
}
1
textbox's don't use viewstate. Your answer is here: stackoverflow.com/questions/16064621/…dcaswell
@user814064 Thanks. I read that post before. If txtbox doesn't use viewstate, how do I make it not retain the value after clicking the button...defaultuser
@user814064 - TextBox does use viewstate. Read your link. It says viewstate is not the only way textbox retains it's value.afzalulh

1 Answers

1
votes

To clear out a text box, set its Text property to null or "":

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 = "";
}