關於asp.net中綁定的模版列CheckBox無法觸發OnCheckedChanged事件的問題

來源:互聯網
上載者:User

以下是代碼:

1 <ISWebGrid:WebGridColumn Caption="首頁顯示" InputRequired="true" Name="Title" Width="70px"
2 ColumnType="Template" DataMember="IsShow" EditType="NoEdit">
3 <CellTemplate>
4 <asp:CheckBox runat="server" ID="cbIsShow" Checked='<%# Bind("IsShow") %>' EnableViewState="true"
5 OnCheckedChanged="cbIsShow_CheckedChanged" Enabled="false" />
6 </CellTemplate>
7 </ISWebGrid:WebGridColumn>
1 protected void cbIsShow_CheckedChanged(object sender,EventArgs e)
2 {
3 Response.Write("<script>alert(\"cbIsShow_CheckedChanged\");</script>");
4 }

當點擊模版列的CheckBox時,背景cbIsShow_CheckedChanged方法總是不執行。

google中有許多種答案如:

1.AutoPostBack="True"沒寫;

2.EnableViewState要設為false;

試過後都不行。

當我把綁定去掉以後,cbIsShow_CheckedChanged方法正常運行。

我給的解釋是:

OnCheckedChanged是在CheckBox的Checked屬性變化之後才會滿足事件觸發條件的,模版列中的CheckBox的Checked屬性綁定了IsShow欄位,在沒有改變資料來源的情況下,CheckBox的Checked屬性值是不會改變的,不滿足事件觸發條件,所以不運行事件處理函數也是正常的。

也許可行的方法為:

1.為CheckBox添加用戶端事件onClicked,在js中調用背景方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.