簡體   English   中英

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

}

除了所有的ASP.NET服務器控件ButtonHyperlinkLinkButton有一個默認AutoPostBack的屬性false ,所以,你應該設置AutoPostBack="true"在你的CheckBox

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

只有在我點擊按鈕時才會觸發

正如我所說,這是因為默認情況下Button AutoPostBack屬性為true ,因此在您選中CheckBox然后單擊按鈕后, CheckBox狀態會自動回發到服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM