2

I designed a simple page with, two text boxes, one checkbox, one button, and one label.

When I start I want to check the checkbox to make the button enabled, and then enter two numbers into the two textboxes, click the button to do addition, and show the result in the label.

But when I click the checkbox the page postback is not working; it's not writing Page is posted back on the page and the button is still disabled.

However, if I make the button enabled and do the addition it invokes the page postback and also invokes the checkedchanged method.

<asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSecond" runat="server"></asp:TextBox>
<asp:Label ID="result" runat="server"></asp:Label>

<td>
    <asp:CheckBox ID="cboptions" runat="server" AutoPostBack="True"   
        onCheckedChanged="cboptions_CheckedChanged" />
</td>

<asp:Button ID="submit" runat="server" Text ="addition" onclick="Button_Click"/>

Code:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack == true)
    {
        Response.Write("Page is posted back");
    }
}
protected void cboptions_CheckedChanged(object sender, EventArgs e)
{
    submit.Enabled = cboptions.Checked;
}
protected void submit_Click(object sender, EventArgs e)
{
    int a = Convert.ToInt32(txtFirst.Text);
    int b = Convert.ToInt32(txtSecond.Text)+a;
    result.Text = b.ToString();
}
James Skemp
  • 8,018
  • 9
  • 64
  • 107
Huizhong Xu
  • 35
  • 1
  • 1
  • 6

1 Answers1

4

There were many formatting errors in your code, do it this way

Aspx

 <asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSecond" runat="server"></asp:TextBox>
<asp:Label ID="result" runat="server"></asp:Label>
<asp:CheckBox ID="cboptions" runat="server" AutoPostBack="True" 
  onCheckedChanged="cboptions_CheckedChanged" />
<asp:Button ID="btn" runat="server" Text ="addition" onclick="Button_Click"/>

C#

 protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == true)
            {
                Response.Write("Page is posted back");
            }
        }

        protected void cboptions_CheckedChanged(object sender, EventArgs e)
        {
            btn.Enabled = cboptions.Checked;
        }
        protected void Button_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(txtFirst.Text);
            int b = Convert.ToInt32(txtSecond.Text) + a;
            result.Text = b.ToString();
        }
Sid M
  • 4,354
  • 4
  • 30
  • 50
  • thx, i noticed the method name is button_click, i changed that to submit_click now, problem still exists, i didn't paste all the things cause i don't think the tds and trs are the problem here, i checked all those small things they are alright, i only pasted the important codes, but still thx – Huizhong Xu Jul 29 '14 at 14:43
  • @HuizhongXu copy paste the whole code and after that you can add your td and tr in between where ever you want to. – Sid M Jul 29 '14 at 14:45
  • @HuizhongXu did it worked or you are still getting some error? i did many changes in your code.. – Sid M Jul 29 '14 at 14:52
  • yours worked, mine doesn't after i changed the onclick = "submit_Click", still don't see why my code is wrong though – Huizhong Xu Jul 29 '14 at 15:16
  • BTW, in my code button has Enabled = "false", i didn't write it here – Huizhong Xu Jul 29 '14 at 15:19
  • can you tell me why i am wrong, i just want to know which part is the problem – Huizhong Xu Jul 29 '14 at 15:20
  • yeah enable =false won't create a problem, don't use `submit` word as its reserved keyword. that will solve all your problem. There were different function names too on aspx and in code behind, make sure you use the same names. – Sid M Jul 29 '14 at 15:22