CheckedChanged 事件未触发

CheckedChanged event is not fired

在我的 WebForm 中,我希望在用户检查一种产品时更改总价,但我的代码存在这个问题,

当我检查 CheckBox

时,CheckedChanged 事件没有触发

它仅在我单击 Button(用作清除按钮)时触发,并且我没有在按钮事件中包含该代码!

这是我的代码:

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

}

ButtonHyperlinkLinkButton 之外的所有 ASP.NET 服务器控件的默认 AutoPostBack 属性 为 false , 所以你应该在 CheckBox:

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

It is firing only when I click the button

正如我所说,这是因为 Button 默认情况下有 AutoPostBack 属性 个 true,所以在您选中 CheckBox 然后单击按钮 CheckBox 状态自动发回服务器。