473,506 Members | 17,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
2 6744
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2221
by: oafyuf | last post by:
Hi, I'm trying to learn ASP.NET by doing a pilot project: I have a DataGrid which contains a nested DataList. I want to iterate through the SQLDataReader for the DataGrid and populate each...
0
986
by: Steve R | last post by:
I have a Datagrid with several TemplateColumns. I am creating ALL (BoundColumn & TemplateColumn) of the columns for the DataGrid during the PAGE_INIT. One TemplateColumn is a checkbox that allows...
1
1131
by: Steve R | last post by:
I have a Datagrid with several TemplateColumns. I am creating ALL (BoundColumn & TemplateColumn) of the columns for the DataGrid during the PAGE_INIT. One TemplateColumn is a checkbox that allows...
0
1490
by: Alexandre Soares | last post by:
Hi, I'm trying to get the client side id of a control inside a datagrid (ex. myDatagridId_ctl2_myTextBoxId). On the server side code, if I try to use...
2
4011
by: Vincent Mouton | last post by:
Hi, I have never before encountered this problem. I'm trying to retreive the (unique) ClientID from a TextBox inside an ItemTemplate of a Repeater. The purpose is to add it to a Button in that...
3
2835
by: WebMatrix | last post by:
I am struggling with implementing somewhat complicated UI web-control. I explored Repeater, but I am not sure if it's the best way to go. I am leaning towards writing my own custom control and...
2
1497
by: Neo Geshel | last post by:
After pouring over about a dozen sites that clearly dealt with ClientID all by itself, I came to the realization that about 2/3+ of them were doing it wrong. It is indeed impossible to grab the...
0
1436
by: Gujju | last post by:
Hi all, Have a question from the following post to create user control in a Datagrid.... http://www.dotnet247.com/247reference/msgs/45/225337.aspx I have a question.... I can create the...
4
4137
by: EvelynAnd Ethan | last post by:
Hi, ItemCommand event not firing from a dynamic user control ,WHERE A DATAGRID HAS BUTTON,when i click on the linkbutton first time the itemcommand event doesnt fire,second time event fires up ...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.