Valerie Meunier

Valerie Meunier

  • 1.8k
  • 248
  • 7.1k

how to keep value when clicking on button in a form?

Oct 15 2021 11:10 AM


there are 2 times two textbox for entering two numbers and two times a button to make the sum of them.

When i click on the second button for making the sum of the 3th and 4th textbox, the sum of the first and second textbox disappears.

How can i keep the first sum visible while i click on the second button? I tried with EnableViewState="true" but no succes.



My code:

<asp:Label ID="Label2" runat="server" Text="First number:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="25px"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Second number:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Width="25px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Sum" OnClick="Button1_Click" />

<asp:Label ID="Label5" runat="server" Text="First number:"></asp:Label>&nbsp;&nbsp;
<asp:TextBox ID="TextBox3" runat="server" Width="25px"></asp:TextBox></td>
<asp:Label ID="Label6" runat="server" Text="Second number:"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server" Width="25px"></asp:TextBox></td>
<asp:Button ID="Button3" runat="server" Text="Sum" OnClick="Button2_Click" />

 protected void Button1_Click(object sender, EventArgs e)
        if (int.TryParse(TextBox1.Text, out g1) && int.TryParse(TextBox2.Text, out g2))
            int sum = g1 + g2;
            Label4.Text = sum.ToString();

protected void Button2_Click(object sender, EventArgs e)
        if (int.TryParse(TextBox3.Text, out g1) && int.TryParse(TextBox4.Text, out g2))
            int sum = g1 + g2;
            Label7.Text = sum.ToString();

Answers (4)