Events involved with Checkbox in datagrid?

L

Lars Netzel

If I put a checkbox in a datagrid (ASP.NET) and set the Autopostback to true
I can catch OnChange event on checkbox but how do I then catch what
DataGridItemIndex is?

Can I use some Event in the Datagrid that will fire off when the Checkbox is
changed?

regards
/Lars
 
E

Eliyahu Goldin

Lars,

You can set up a grid event, as you are suggesting, but it is rather
complicated. You would need to add a hidden button column and to setup
events on client side. I would consider two simpler solutions:

Solution 1. Typecast the sender argument in the OnChange event handler to
CheckBox, get the value of UniqueId property and parse it. The UniqueId is
generated automatically out of the id of the datagrid, the id of the
checkbox and a prefix like "Ctl1", "Ctl2" etc. Once you get the prefix, you
know the number of the datarow where the checkbox is.

Solution 2. Navigate in control hierachy. sender's Parent should point you
to the datagrid row.

Eliyahu
 
L

Lars Netzel

I actully did this instead but it's not working really well... though it
seems liek tha tright way to go!

In ItemCreated of the Grid I added a AddHandler for a Checkbox.CheckChanged
that I get in ItemCreated which calls for a specific function, that work
nice BUT the first time I click one of the Checkboxes I fired ALL checkboxes
in that column.. then after that I only get One, the right one. PostBack is
True in all the events (off course) so I have no idea why I trigger ALL the
checkboxed CheckChanged the first time only.

/Lars
 
S

Scott Allen

I'd definitely lean towards solution 2, as it can be much cleaner and
doesn't depend on the details of how controls are named.
 
S

Scott Allen

Hi Lars:

Let's say you have the following control inside of a DataGrid
TemplateColumn:

<asp:CheckBox Runat="server" ID="MyCheckBox"
OnCheckedChanged="MyCheckBox_CheckChanged" AutoPostBack="True"/>

You can write an event handler to get the index like so:

protected void MyCheckBox_CheckChanged(object source, EventArgs e)
{
Control c = (Control)source;
DataGridItem item = (DataGridItem)c.Parent.Parent;

Response.Write(item.ItemIndex);
}


Notice you need to go to the parent of the parent of the check box
control. You can see this if you dump the control hierarchy:

MyCheckBox (System.Web.UI.WebControls.CheckBox) ->
(System.Web.UI.WebControls.TableCell) ->
(System.Web.UI.WebControls.DataGridItem) ->
(System.Web.UI.WebControls.DataGridTable) ->
DataGrid1 (System.Web.UI.WebControls.DataGrid) ->
Form1 (System.Web.UI.HtmlControls.HtmlForm) ->
(ASP.FindControl4pre_aspx)

The CheckBox belongs to a TableCell control, which belongs to a
DataGridItem control, etc. etc. I have code to dump the hierarchy in
the following article:
http://odetocode.com/Articles/116.aspx

HTH,
 
L

Lars Netzel

I don't understand...

I have Fixed my problem using the AddHandler in ItemCreated thingy which
works really good now but what do you mean with this Sender's Parent?

In what Event do I navigate the Controls?

/Lars
 
S

Scott Allen

Hi Lars:

I'm glad you found another solution. I thought you were still looking
for a way to find the item index.
 
E

Eliyahu Goldin

Lars,

When the CheckBox's CheckedChanged event handler gets called, it gets 2
parameters. sender is the first of them. It is the reference to the control
that fired the event.

Eliyahu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top