Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnCheckedChanged event not firing in GridView at all

I have a GridView with an asp CheckBox in a TemplateField. The TemplateField is defined as follows:

<asp:TemplateField HeaderText="HeaderName">
    <ItemTemplate>
        <asp:CheckBox ID="checkBoxId" runat="server" OnCheckedChanged="MyCheckChangedMethod" AutoPostBack="true"/>
    </ItemTemplate>
</asp:TemplateField>

When I run my web project with a breakpoint inside MyCheckChangedMethod and click the checkbox nothing happens. The breakpoint is not hit. My Visual Studio debugger is running.

Additionally, I have AutoEventWireup = True in my page defnition so I don't have to manually hook up the event. I have never had a problem doing it this way before. I have a button on the same page setup the exact same way with a click event and the breakpoint gets hit fine in that.

Any ideas?

like image 360
Adam Avatar asked Aug 17 '09 16:08

Adam


3 Answers

The problem occurs when DataBind is called before the control event is firing. If you call DataBind in Page_Load put it in if (!isPostBack) {} and call DataBind in the event handler itself.

like image 105
Eduard Grinberg Avatar answered Nov 10 '22 19:11

Eduard Grinberg


You need to add AutoPostback = True in asp:CheckBox tag.

like image 20
royalnight102 Avatar answered Nov 10 '22 19:11

royalnight102


The postback event for the checkbox control won't fire correctly because it's within a GridView that mangles the ID of the control.

If you need the Checkbox to reflect data you can use the CheckBoxField object and bind that way.

If you need it perform an action for the row, you may want to look at the ButtonField object using the CommandName property and the RowCommand event.

There are ways to access the checkboxes within the GridView server side.

like image 5
CAbbott Avatar answered Nov 10 '22 18:11

CAbbott