By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,173 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,173 IT Pros & Developers. It's quick & easy.

Events involved with Checkbox in datagrid?

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 18 '05 #2

P: n/a
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
"Eliyahu Goldin" <re*************@monarchmed.com> skrev i meddelandet
news:%2****************@tk2msftngp13.phx.gbl...
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

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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


Nov 18 '05 #3

P: n/a
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.

--
Scott
http://www.OdeToCode.com

On Tue, 13 Jul 2004 12:03:31 +0200, "Eliyahu Goldin"
<re*************@monarchmed.com> wrote:
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

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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


Nov 18 '05 #4

P: n/a
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,

--
Scott

On Tue, 13 Jul 2004 12:54:30 +0200, "Lars Netzel"
<[stop_spam]@host.topdomain> wrote:
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
"Eliyahu Goldin" <re*************@monarchmed.com> skrev i meddelandet
news:%2****************@tk2msftngp13.phx.gbl...
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

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Nov 18 '05 #5

P: n/a
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


"Scott Allen" <bitmask@[nospam].fred.net> skrev i meddelandet
news:lk********************************@4ax.com...
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.

--
Scott
http://www.OdeToCode.com

On Tue, 13 Jul 2004 12:03:31 +0200, "Eliyahu Goldin"
<re*************@monarchmed.com> wrote:
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 isgenerated 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, youknow the number of the datarow where the checkbox is.

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

Eliyahu

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
Checkboxis
changed?

regards
/Lars

Nov 18 '05 #6

P: n/a
Hi Lars:

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

--
Scott
http://www.OdeToCode.com

On Tue, 13 Jul 2004 15:06:07 +0200, "Lars Netzel"
<[stop_spam]@host.topdomain> wrote:
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


Nov 18 '05 #7

P: n/a
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

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:Om**************@TK2MSFTNGP09.phx.gbl...
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


"Scott Allen" <bitmask@[nospam].fred.net> skrev i meddelandet
news:lk********************************@4ax.com...
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.

--
Scott
http://www.OdeToCode.com

On Tue, 13 Jul 2004 12:03:31 +0200, "Eliyahu Goldin"
<re*************@monarchmed.com> wrote:
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 toCheckBox, get the value of UniqueId property and parse it. The UniqueId isgenerated 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, youknow the number of the datarow where the checkbox is.

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

Eliyahu

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> If I put a checkbox in a datagrid (ASP.NET) and set the Autopostback totrue
> 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 Checkboxis
> changed?
>
> regards
> /Lars
>
>


Nov 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.