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

How to get ClientID of a control added in the DataGrid ItemCreated event?

P: n/a
DC
Hi,

I am doing something like this in the ItemCreated event (ASP.Net 1.1):

DataGridItem pagerRow = e.Item;
TableCell pagerCell = pagerRow.Cells[0];
Control addedControl = new Control();
addedControl.ID = "addedControl";
pagerCell.Controls.Add(addedControl);
Now I want to include some Javascript code that is using addedControl
and I therefore need to know the ClientID of addedControl.

The only way I found to get the ClientID is to locate (by traversing
the tree in the DataGrid's PreRender event) a control in the DataGrid
control tree whose name ends with "addedControl". I will then be able
to get the ClientID which usully looks like MyGrid_ctl0_addedControl or
something like this.

But this is so lame. I would be thankful for any hint on a better
approach.

Regards
DC

Sep 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

DC wrote:
Hi,

I am doing something like this in the ItemCreated event (ASP.Net 1.1):

DataGridItem pagerRow = e.Item;
TableCell pagerCell = pagerRow.Cells[0];
Control addedControl = new Control();
addedControl.ID = "addedControl";
pagerCell.Controls.Add(addedControl);
Now I want to include some Javascript code that is using addedControl
and I therefore need to know the ClientID of addedControl.

The only way I found to get the ClientID is to locate (by traversing
the tree in the DataGrid's PreRender event) a control in the DataGrid
control tree whose name ends with "addedControl". I will then be able
to get the ClientID which usully looks like MyGrid_ctl0_addedControl or
something like this.

But this is so lame. I would be thankful for any hint on a better
approach.

Regards
DC
Maybe I am missing the point, but... you have a reference to
addedControl, why can't you use it?

addedControl.ClientID;

If the reference to ClientID is in another method, then store
addedControl as a page's member variable. If it must survive a postback,
then store it in the Session (or just store the ClientID in the Session,
it won't change between postbacks anyway unless you modify the page's
structure).

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 29 '06 #2

P: n/a
DC
Hi Laurent,

I need the ClientID in another method (because in ItemCreated the
ClientID will simply be "addedControl", but in PreRender it has it's
fully qualified name).

I therefore tried keeping a reference in the page:

(on page level)
private Control addedControl;

.... (in ItemCreated)
addedControl = new Control();
addedControl.ID = "addedControl";
pagerCell.Controls.Add(addedControl);

That did not work. Actually it looks like the Controls.Add clones
addedControl and adds the clone, because I will find two controls with
ID "..._addedControl" in the DataGrid after this. I have not found a
way to store a reference to the actually added control on page level.

Regards
DC
Laurent Bugnion wrote:
Hi,

DC wrote:
Hi,

I am doing something like this in the ItemCreated event (ASP.Net 1.1):

DataGridItem pagerRow = e.Item;
TableCell pagerCell = pagerRow.Cells[0];
Control addedControl = new Control();
addedControl.ID = "addedControl";
pagerCell.Controls.Add(addedControl);
Now I want to include some Javascript code that is using addedControl
and I therefore need to know the ClientID of addedControl.

The only way I found to get the ClientID is to locate (by traversing
the tree in the DataGrid's PreRender event) a control in the DataGrid
control tree whose name ends with "addedControl". I will then be able
to get the ClientID which usully looks like MyGrid_ctl0_addedControl or
something like this.

But this is so lame. I would be thankful for any hint on a better
approach.

Regards
DC

Maybe I am missing the point, but... you have a reference to
addedControl, why can't you use it?

addedControl.ClientID;

If the reference to ClientID is in another method, then store
addedControl as a page's member variable. If it must survive a postback,
then store it in the Session (or just store the ClientID in the Session,
it won't change between postbacks anyway unless you modify the page's
structure).

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.