简体   繁体   English

在GridView中时oncheckedchanged不触发CheckBox

[英]oncheckedchanged not firing for CheckBox when in GridView

I'm trying to have a OnCheckedChanged event fire for an asp:CheckBox which is within a GridView. 我正在尝试为GridView中的asp:CheckBox触发OnCheckedChanged事件。 But when I put it into a GridView it does not fire the OnCheckedChanged in the code-behind. 但是,当我将其放入GridView时,它不会在后面的代码中触发OnCheckedChanged。

If I put the same checkbox outside of the gridview, it's perfectly fine. 如果我将相同的复选框放在gridview之外,那就很好。

I've tried setting the function as a RowCommand and that doesn't work either. 我试过将函数设置为RowCommand ,但也不起作用。

Here is my checkbox: 这是我的复选框:

<asp:CheckBox ID="HoldBillingCheckBox" runat="server" key='<%# Eval("CustomerItemID") %>' Checked='<%# Eval("HoldBilling") %>' OnCheckedChanged="CustomerItemsGridView_CheckChanged" Enabled="true" EnableViewState="true" AutoPostBack="true"/>

It seems to me that this only occurs when you are trying to wrap the CheckBox with any view. 在我看来,这仅在您尝试使用任何视图包装CheckBox时发生。

Does anyone have any suggestions? 有没有人有什么建议? I've googled so many different solutions but nothing seems to work (they are mainly telling me to add AutoPostBack as true ...or enableviewstate as true). 我已经用Google搜索了很多不同的解决方案,但是似乎没有任何效果(它们主要是告诉我将AutoPostBack添加为true ...或将enableviewstate添加为true)。

thanks in advance! 提前致谢!

If you are rebinding your gridview on each load of the page, then you are destroying the statefulness of the CheckChanged event. 如果要在页面的每次加载上重新绑定gridview,则将破坏CheckChanged事件的状态。 When you rebind your grid, it invalidates any controls that were in the previous "version" of the page. 重新绑定网格时,它会使页面上一个“版本”中的所有控件无效。

You need to add if (!IsPostBack) as a conditional to prevent your GridView from rebinding. 您需要添加if (!IsPostBack)作为条件,以防止GridView重新绑定。

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

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