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

D

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
 
L

Laurent Bugnion

Hi,
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
 
D

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 said:
Hi,
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
 

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

Top