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
 
Joined
Nov 9, 2006
Messages
1
Reaction score
0
hi,

Not only ClientID does not contain the "real" client id but also, if you access it it will be generate (on the client side) as "addedControl" which will result in having a list of controls with the exact same name. Not to mention that the viewstate will not work properly also.

I had the same needs. Add javascript to checkboxes. So, I used the PreRender event of the repeater in which I did something that looks like:

For Each item as System.Web.UI.WebControls.RepeaterItem In MyRepeater.Items
Dim myCheckBox as System.web.ui.webcontrols.checkbox = _
CType(item.findcontrol("mycheckboxname"), System.web.ui.webcontrols.checkbox )
mycheckbox.attributes.add("onclick","....")
Next


In this code, you can reference the clientid and it will return the right one.

Regards,

DC said:
Hi Laurent,

I had the


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
 

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,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top