CheckedChanged event not fired

In my WebForm I want the total price to change when a user checks a single product, but I have this problem with my code,

The CheckedChanged event CheckedChanged not fire when I check the CheckBox

It is executed only when the Button pressed (used as the "Clear" button), and I did not include this code in the button event!

Here is my code:

 public partial class _Default : System.Web.UI.Page { int total = 0; String strtotal; protected void ckb1_CheckedChanged(object sender, EventArgs e) { if (ckb1.Checked) { total = total + 100; strtotal = total.ToString(); lbl2.Text = strtotal; } } protected void ckb2_CheckedChanged(object sender, EventArgs e) { if (ckb2.Checked) { total = total + 80; strtotal = total.ToString(); lbl2.Text = strtotal; } } protected void ckb3_CheckedChanged(object sender, EventArgs e) { if (ckb3.Checked) { total = total + 70; strtotal = total.ToString(); lbl2.Text = strtotal; } } protected void Button3_Click(object sender, EventArgs e) { TextBox1.Text = " "; ckb1.Checked = false; ckb2.Checked = false; ckb3.Checked = false; } } 
+5
source share
1 answer

All ASP.NET Server controls, except Button , Hyperlink and LinkButton , have the AutoPostBack property AutoPostBack by default, so you must set AutoPostBack="true" in the CheckBox :

 <asp:CheckBox ID="ckb1" runat="server" AutoPostBack="true" OnCheckedChanged="ckb1_CheckedChanged" /> 

Only performed when a button is pressed

As I said, this is because by default the Button has the AutoPostBack property true , so after you checked the CheckBox and then click the button, the CheckBox state will automatically be sent back to the server.

+5
source

All Articles