2015-09-15 61 views
5

在我WebForm我想總價格變化時,用戶檢入一個產品,但是我有這個問題,我的代碼,的CheckedChanged事件不會觸發

CheckedChanged事件在不觸發,當我檢查CheckBox

只有當我點擊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;  
    } 

} 

回答

5

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

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

只有當我點擊按鈕

正如我所說的,這是因爲Button有默認的trueAutoPostBack屬性,以便您檢查CheckBox,然後點擊按鈕CheckBox狀態自動回發到服務器後。

+0

非常感謝! –