0

I'm trying to have a OnCheckedChanged event fire for an asp:CheckBox which is within a GridView. But when I put it into a GridView it does not fire the OnCheckedChanged in the code-behind.

If I put the same checkbox outside of the gridview, it's perfectly fine.

I've tried setting the function as a RowCommand and that doesn't work either.

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.

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).

thanks in advance!

Blachshma
  • 17,097
  • 4
  • 58
  • 72
w1ck3d64
  • 402
  • 2
  • 8
  • 15

1 Answers1

1

If you are rebinding your gridview on each load of the page, then you are destroying the statefulness of the CheckChanged event. 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.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • you mean in Page_Load? But the event won't even fire. It doesn't even hit my break points at all within the OnCheckedChanged function. – w1ck3d64 Mar 06 '13 at 21:22
  • You should post the code where your GridView is bound. If you're rebinding it on each post, then you're basically undoing any event bindings on any controls in the GridView. – Joel Etherton Mar 06 '13 at 21:27
  • technically..if i remove the autopostback and have no bind..it should still fire my function right? i removed postbacks and rebinds but it still doesn't fire my function – w1ck3d64 Mar 06 '13 at 21:34
  • No, you'll need postbacks for the control to perform an event. The rebind would be what is killing it. – Joel Etherton Mar 06 '13 at 21:41
  • yeah that's right i need the postbacks to fire server events..hmm but my databind is only done through page load...all i have for the gridview is the aspx and the datasource (you guys can probably tell i'm kinda new to this..so sorry for the questions) – w1ck3d64 Mar 07 '13 at 15:59
  • Please post the code showing the Page_Load event. The Page_Load event fires every time a request occurs to the page (get or post). If you do not filter out "PostBack" requests, your Page_Load will rebind your gridview. This will destroy your original checkbox and all event transactions will be lost. – Joel Etherton Mar 07 '13 at 16:11