简体   繁体   English

CheckedChanged事件未被触发

[英]CheckedChanged event is not fired

In my WebForm I want the total price changed when the user check one product but I have this problem with my code , 在我的WebForm我希望在用户检查一个产品时总价格发生变化,但我的代码存在此问题,

the CheckedChanged event is not firing when I check the CheckBox 检查CheckBox时, CheckedChanged事件没有触发

It is firing only when I click the Button (used for as clear button) , and I didn't include that code within the button event ! 它只在我单击Button (用作清除按钮)时触发,并且我没有在按钮事件中包含该代码!

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

}

All ASP.NET Server controls except Button , Hyperlink and LinkButton have a default AutoPostBack property of false , So you should set AutoPostBack="true" in your CheckBox : 除了所有的ASP.NET服务器控件ButtonHyperlinkLinkButton有一个默认AutoPostBack的属性false ,所以,你应该设置AutoPostBack="true"在你的CheckBox

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

It is firing only when I click the button 只有在我点击按钮时才会触发

As I said this is because the Button have AutoPostBack property of true by default so after you checked the CheckBox and then click the button the CheckBox state automatically posts back to the server. 正如我所说,这是因为默认情况下Button AutoPostBack属性为true ,因此在您选中CheckBox然后单击按钮后, CheckBox状态会自动回发到服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM