OnCheckedChanged event not firing in GridView at all
Asked Answered
I

4

5

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?

Imaginable answered 17/8, 2009 at 16:56 Comment(0)
E
5

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.

Enravish answered 17/8, 2009 at 19:9 Comment(0)
M
6

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

Marcos answered 16/12, 2009 at 2:14 Comment(0)
E
6

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.

Epstein answered 13/4, 2011 at 11:52 Comment(0)
E
5

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.

Enravish answered 17/8, 2009 at 19:9 Comment(0)
K
0

try:

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

Make sure that the aspx page has CodeFile="YOUR_FILE.aspx.cs" at the top.

Also see to it that your function MyCheckChangedMethod is defined as

Function should have object sender, EventArgs e.

public void MyCheckChangedMethod(object sender, EventArgs e)
{
  bool b = false;//your data here
}

Also make sure that the web.config has debug set to true (think already done).

Killiecrankie answered 17/8, 2009 at 17:23 Comment(2)
The codefile is in the header of the file. Like I said at the bottom, my button click event works fine, it's just the CheckBox inside the GridView that's giving me problems. Also, the code you suggested is the exact same as the way my checkbox is defined above. And the method is defined properly with the proper method signature with object and EventArgs. This is why the problem is so frustrating. I know how to set up an event through ASP.net markupImaginable
Was just a suggestion. I tried the code my self (sans the grid view) which worked. Does the function contain some code or is it empty?Killiecrankie

© 2022 - 2024 — McMap. All rights reserved.