473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Identifying the element ID in a client script from a WebControl

kw
What is the proper way to get the element ID for a client script?

For example, suppose in the WebControl:
TextBox t=new TextBox;
t.ID=this.ClientID+"X";
....
Then elsewhere we want to access it on the client:
Control.Attributes["OnClick"] =
"javascript:document.getElementById('"+this.Client ID+"X"+"').value=
'test';";

It doesn't work because the textbox ID becomes something like:

TextBox1_X

and the link control looks like this:

javascript:document.getElementById('TextBox1X').va lue= 'test';


Nov 18 '05 #1
9 2636
ClientId will get rendered as the id, UniqueId as the name...in your
getElementById example, you'd want to use ClientId

"javascript:document.getElementById('" + t.ClientId+ "').value = 'test';"

you don't have to give your textbox a special id, it'll automatically get
one assigned.

The problem with your code is that you append the "X" in the getElementbyId
field..by setting the ID property of the control, the ClientId is
automatically updated.

Karl

"kw" <el*****************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
What is the proper way to get the element ID for a client script?

For example, suppose in the WebControl:
TextBox t=new TextBox;
t.ID=this.ClientID+"X";
...
Then elsewhere we want to access it on the client:
Control.Attributes["OnClick"] =
"javascript:document.getElementById('"+this.Client ID+"X"+"').value=
'test';";

It doesn't work because the textbox ID becomes something like:

TextBox1_X

and the link control looks like this:

javascript:document.getElementById('TextBox1X').va lue= 'test';

Nov 18 '05 #2
kw
Hi Karl, that doesn't work either. In the example below I've loaded a
WebControl into a dynamically created UserControl on a WebPage. The
ClientID returned is different from what finally appears on the control:

<input name="_ctl0:pg:_ctl4:_ctl0" type="text" value=""
OnFocus="javascript:document.getElementById('pg__X ').value= 'test'>
<textarea name="_ctl0:pg:_X" rows="3" id="_ctl0_pg__X"></textarea>

"Karl" <none> wrote in message news:en**************@tk2msftngp13.phx.gbl...
ClientId will get rendered as the id, UniqueId as the name...in your
getElementById example, you'd want to use ClientId

"javascript:document.getElementById('" + t.ClientId+ "').value = 'test';"

you don't have to give your textbox a special id, it'll automatically get
one assigned.

The problem with your code is that you append the "X" in the getElementbyId field..by setting the ID property of the control, the ClientId is
automatically updated.

Karl

"kw" <el*****************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
What is the proper way to get the element ID for a client script?

For example, suppose in the WebControl:
TextBox t=new TextBox;
t.ID=this.ClientID+"X";
...
Then elsewhere we want to access it on the client:
Control.Attributes["OnClick"] =
"javascript:document.getElementById('"+this.Client ID+"X"+"').value=
'test';";

It doesn't work because the textbox ID becomes something like:

TextBox1_X

and the link control looks like this:

javascript:document.getElementById('TextBox1X').va lue= 'test';


Nov 18 '05 #3
Can I see code as to how it's being added? I've played with adding it in a
placeholder, in a grid....can't reproduce the mismatch..

Karl

"kw" <el*****************@hotmail.com> wrote in message
news:eB**************@TK2MSFTNGP10.phx.gbl...
Hi Karl, that doesn't work either. In the example below I've loaded a
WebControl into a dynamically created UserControl on a WebPage. The
ClientID returned is different from what finally appears on the control:

<input name="_ctl0:pg:_ctl4:_ctl0" type="text" value=""
OnFocus="javascript:document.getElementById('pg__X ').value= 'test'>
<textarea name="_ctl0:pg:_X" rows="3" id="_ctl0_pg__X"></textarea>

"Karl" <none> wrote in message

news:en**************@tk2msftngp13.phx.gbl...
ClientId will get rendered as the id, UniqueId as the name...in your
getElementById example, you'd want to use ClientId

"javascript:document.getElementById('" + t.ClientId+ "').value = 'test';"
you don't have to give your textbox a special id, it'll automatically get one assigned.

The problem with your code is that you append the "X" in the

getElementbyId
field..by setting the ID property of the control, the ClientId is
automatically updated.

Karl

"kw" <el*****************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
What is the proper way to get the element ID for a client script?

For example, suppose in the WebControl:
TextBox t=new TextBox;
t.ID=this.ClientID+"X";
...
Then elsewhere we want to access it on the client:
Control.Attributes["OnClick"] =
"javascript:document.getElementById('"+this.Client ID+"X"+"').value=
'test';";

It doesn't work because the textbox ID becomes something like:

TextBox1_X

and the link control looks like this:

javascript:document.getElementById('TextBox1X').va lue= 'test';



Nov 18 '05 #4
kw
Thanks Karl. I wish I could post you the code for the control, but it would
surely only confuse the issue because it's a very complex compound
WebControl.

The gist of it is this: OnLoad() -calls-> ControlCreation (wherein onClick
Attribute is set thus we will need to know the ID of the control) -calls->
creation of the final control in the WebControl (wherein the ID must be
assigned).

So ControlCreation looks like this:
Control.Attributes["OnClick"] =
"javascript:document.getElementById('"+this.Client ID+"X"+"').value='test';";

And the final stage looks like this:
TextBox t=new TextBox;
t.ID=this.ClientID+"X";

Now this strategy works *some* of the time, but not when the WebControl is
in a UserControl on a WebForm. The ID of the final control looks like
_ctl0_pg__X and not pg__X and I don't see any way for the webcontrol to know
what it's final ID is.

<input name="_ctl0:pg:_ctl4:_ctl0" type="text" value=""
OnClick="javascript:document.getElementById('pg__X ').value= 'test'>
<textarea name="_ctl0:pg:_X" rows="3" id="_ctl0_pg__X"></textarea>

>
> It doesn't work because the textbox ID becomes something like:
>
> TextBox1_X
>
> and the link control looks like this:
>
> javascript:document.getElementById('TextBox1X').va lue= 'test';
>

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:us**************@TK2MSFTNGP09.phx.gbl...
Can I see code as to how it's being added? I've played with adding it in a
placeholder, in a grid....can't reproduce the mismatch..

Karl

"kw" <el*****************@hotmail.com> wrote in message
news:eB**************@TK2MSFTNGP10.phx.gbl...
Hi Karl, that doesn't work either. In the example below I've loaded a
WebControl into a dynamically created UserControl on a WebPage. The
ClientID returned is different from what finally appears on the control:

<input name="_ctl0:pg:_ctl4:_ctl0" type="text" value=""
OnFocus="javascript:document.getElementById('pg__X ').value= 'test'>
<textarea name="_ctl0:pg:_X" rows="3" id="_ctl0_pg__X"></textarea>

"Karl" <none> wrote in message

news:en**************@tk2msftngp13.phx.gbl... ClientId will get rendered as the id, UniqueId as the name...in your
getElementById example, you'd want to use ClientId

"javascript:document.getElementById('" + t.ClientId+ "').value = 'test';"
you don't have to give your textbox a special id, it'll automatically get one assigned.

The problem with your code is that you append the "X" in the

getElementbyId
field..by setting the ID property of the control, the ClientId is
automatically updated.

Karl

"kw" <el*****************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> What is the proper way to get the element ID for a client script?
>
> For example, suppose in the WebControl:
> TextBox t=new TextBox;
> t.ID=this.ClientID+"X";
> ...
> Then elsewhere we want to access it on the client:
> Control.Attributes["OnClick"] =
> "javascript:document.getElementById('"+this.Client ID+"X"+"').value=
> 'test';";
>
> It doesn't work because the textbox ID becomes something like:
>
> TextBox1_X
>
> and the link control looks like this:
>
> javascript:document.getElementById('TextBox1X').va lue= 'test';
>
>
>
>



Nov 18 '05 #5
I'm being an idiot..get the ClientId AFTER you add it to the control
collection...
Dim t As New TextBox
string firstClientId = t.clientId
plc.Controls.Add(t)
string secondClientId = t.clientId
(that's a c#/vb.net mix :) ) anyways...secondClientId won't be equal to
firstClientId : ) when you add it to a control, the parent'd id gets
appended, which is what you are missing

Karl

"kw" <el*****************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Thanks Karl. I wish I could post you the code for the control, but it would surely only confuse the issue because it's a very complex compound
WebControl.

The gist of it is this: OnLoad() -calls-> ControlCreation (wherein onClick
Attribute is set thus we will need to know the ID of the control) -calls->
creation of the final control in the WebControl (wherein the ID must be
assigned).

So ControlCreation looks like this:
Control.Attributes["OnClick"] =
"javascript:document.getElementById('"+this.Client ID+"X"+"').value='test';";
And the final stage looks like this:
TextBox t=new TextBox;
t.ID=this.ClientID+"X";

Now this strategy works *some* of the time, but not when the WebControl is
in a UserControl on a WebForm. The ID of the final control looks like
_ctl0_pg__X and not pg__X and I don't see any way for the webcontrol to know what it's final ID is.

<input name="_ctl0:pg:_ctl4:_ctl0" type="text" value=""
OnClick="javascript:document.getElementById('pg__X ').value= 'test'>
<textarea name="_ctl0:pg:_X" rows="3" id="_ctl0_pg__X"></textarea>

> >
> > It doesn't work because the textbox ID becomes something like:
> >
> > TextBox1_X
> >
> > and the link control looks like this:
> >
> > javascript:document.getElementById('TextBox1X').va lue= 'test';
> >
"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:us**************@TK2MSFTNGP09.phx.gbl...
Can I see code as to how it's being added? I've played with adding it in

a placeholder, in a grid....can't reproduce the mismatch..

Karl

"kw" <el*****************@hotmail.com> wrote in message
news:eB**************@TK2MSFTNGP10.phx.gbl...
Hi Karl, that doesn't work either. In the example below I've loaded a
WebControl into a dynamically created UserControl on a WebPage. The
ClientID returned is different from what finally appears on the control:
<input name="_ctl0:pg:_ctl4:_ctl0" type="text" value=""
OnFocus="javascript:document.getElementById('pg__X ').value= 'test'>
<textarea name="_ctl0:pg:_X" rows="3" id="_ctl0_pg__X"></textarea>

"Karl" <none> wrote in message

news:en**************@tk2msftngp13.phx.gbl...
> ClientId will get rendered as the id, UniqueId as the name...in your
> getElementById example, you'd want to use ClientId
>
> "javascript:document.getElementById('" + t.ClientId+ "').value =

'test';"
>
> you don't have to give your textbox a special id, it'll automatically
get
> one assigned.
>
> The problem with your code is that you append the "X" in the
getElementbyId
> field..by setting the ID property of the control, the ClientId is
> automatically updated.
>
> Karl
>
> "kw" <el*****************@hotmail.com> wrote in message
> news:%2****************@TK2MSFTNGP10.phx.gbl...
> > What is the proper way to get the element ID for a client script?
> >
> > For example, suppose in the WebControl:
> > TextBox t=new TextBox;
> > t.ID=this.ClientID+"X";
> > ...
> > Then elsewhere we want to access it on the client:
> > Control.Attributes["OnClick"] =
> >

"javascript:document.getElementById('"+this.Client ID+"X"+"').value= > > 'test';";
> >
> > It doesn't work because the textbox ID becomes something like:
> >
> > TextBox1_X
> >
> > and the link control looks like this:
> >
> > javascript:document.getElementById('TextBox1X').va lue= 'test';
> >
> >
> >
> >
>
>



Nov 18 '05 #6
kw
Guess what...it still doesn't work for me (I can see how this will work in
your example). And here's the funny thing: after the controls are added and
ClientID is known (even in OnPreRender)...the final HTML looks like this:

MyControl1_MyControl1__ctl16

instead of (from tb.ClientID)

_MyControl1__ctl16

I don't understand why ASP.NET is modifying the ID after OnPreRender.

I'm being an idiot..get the ClientId AFTER you add it to the control
collection...
Dim t As New TextBox
string firstClientId = t.clientId
plc.Controls.Add(t)
string secondClientId = t.clientId
(that's a c#/vb.net mix :) ) anyways...secondClientId won't be equal to
firstClientId : ) when you add it to a control, the parent'd id gets
appended, which is what you are missing

Karl

Nov 18 '05 #7
I'm stumped :(
tb needs to be added to its parent control, but that parent control needs to
be added to its parent control. however the controls are added up in the
final scenario is where you want to get clientid...

Try to output <%=tb.ClientId%> in your page...make sure tb is protected or
less....this will give you the clientId at the very end..if it doesn't match
the cientID you are getting in your codebehind, I'm either being a tard, or
you aren't getting it after all Adds are completed.

Karl

"kw" <el*****************@hotmail.com> wrote in message
news:uB**************@TK2MSFTNGP11.phx.gbl...
Guess what...it still doesn't work for me (I can see how this will work in
your example). And here's the funny thing: after the controls are added and ClientID is known (even in OnPreRender)...the final HTML looks like this:

MyControl1_MyControl1__ctl16

instead of (from tb.ClientID)

_MyControl1__ctl16

I don't understand why ASP.NET is modifying the ID after OnPreRender.

I'm being an idiot..get the ClientId AFTER you add it to the control
collection...
Dim t As New TextBox
string firstClientId = t.clientId
plc.Controls.Add(t)
string secondClientId = t.clientId
(that's a c#/vb.net mix :) ) anyways...secondClientId won't be equal to
firstClientId : ) when you add it to a control, the parent'd id gets
appended, which is what you are missing

Karl


Nov 18 '05 #8
control id and names are a mess because MS did not understand the W3C rules
when they wrote the code.

background.

html form controls have a name property that is used in a form post

<form><input name="myInput" type="button" value="foo"></form>

when posted the browser will send

myInput=foo

originally there were no rules for forming name. later version of html set
rules (use id rule - see below)

later version of html added the id attribute from xml. the id attribute has
many rules.

1) must be unique across the document
2) must start with a letter
3) can only contain letters, numbers and underscore

when ms wrote web controls they had the issue of naming nested controls. the
simple fix concat the names together with ":" between them. for example if
button1 was nested in control control1, the name would be
"control1:button1". this turned out to be a really bad decision. for several
reasons.

1) ":" are not allowed in the id attribute, so they are removed and replaced
with _
2) while non w3c compliant browser allowed the ":" in name, it lead to
scripting errors, because the name was not valid javascript name, so could
not be used in the common syntax:

document.forms[0].myFieldname

3) IE has a bug which causes an alert if a ":" is present in a url under
https. this caused problems with postback:
javascript:_doPostBack('control1:control2')
would cause an alert on an https page, so now __dopostback() replaces
the ":" with $.
javascript:_doPostBack('control1$control2')

4) the name attribute is the name the backend uses to match up the posted
data with the controls. the name still uses ":" even though some proxy
servers will strip it, and thus are not compatiable with .net
now:

ClientID is the name of the control on the server side, it does not have to
be unique. Two parent controls can controls of the same name, otherwise
writing composite controls would be hard.

UniqueID is the what the rendered html attribute id will be. this is what
your javascript should use and will contain the any parent container names.

in your web control you want to:

TextBox t=new TextBox;
t.ID = "X";

Control.Attributes["OnClick"] ="javascript:document.getElementById('"
+ this.FindControl("X").UniqueID + // get the unique id of my
child control X
+ "').value='test';";
assuming the webcontrol's id is TextBox1 (and is a div), when rendered it
looks like:

<div id="TextBox1"
OnClick="javascript:document.getElementById('TextB ox1_X').value='test';" >
<input type=text id="TextBox1_X" name="TextBox1:X">
</div>

-- bruce (sqlwork.com)





"kw" <el*****************@hotmail.com> wrote in message
news:uB**************@TK2MSFTNGP11.phx.gbl...
Guess what...it still doesn't work for me (I can see how this will work in
your example). And here's the funny thing: after the controls are added and ClientID is known (even in OnPreRender)...the final HTML looks like this:

MyControl1_MyControl1__ctl16

instead of (from tb.ClientID)

_MyControl1__ctl16

I don't understand why ASP.NET is modifying the ID after OnPreRender.

I'm being an idiot..get the ClientId AFTER you add it to the control
collection...
Dim t As New TextBox
string firstClientId = t.clientId
plc.Controls.Add(t)
string secondClientId = t.clientId
(that's a c#/vb.net mix :) ) anyways...secondClientId won't be equal to
firstClientId : ) when you add it to a control, the parent'd id gets
appended, which is what you are missing

Karl


Nov 18 '05 #9
kw
BOB BARKER You are the greatest! Not only did it work in the simple cases,
but the most complex nexted dynamically loaded ones too.

I did try UniqueID, but I found it has to be in the OnPreRender phase.
Thus, the script generation has to be the last step.

string s=this.FindControl("X").UniqueID;//should be called from
OnPreRender()
private string GetScript(){
string s=this.FindControl("X").UniqueID;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script>\n");
sb.Append("function DoSomethingReallyImportant(ctl)\n{\n");
sb.Append("document.getElementById(\"");
sb.Append(s);
sb.Append("\").value=\"test\";\n}\n");
sb.Append("</script>\n");
return sb.ToString();
}


"bruce barker" <no***********@safeco.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
control id and names are a mess because MS did not understand the W3C rules when they wrote the code.

background.

html form controls have a name property that is used in a form post

<form><input name="myInput" type="button" value="foo"></form>

when posted the browser will send

myInput=foo

originally there were no rules for forming name. later version of html set
rules (use id rule - see below)

later version of html added the id attribute from xml. the id attribute has many rules.

1) must be unique across the document
2) must start with a letter
3) can only contain letters, numbers and underscore

when ms wrote web controls they had the issue of naming nested controls. the simple fix concat the names together with ":" between them. for example if
button1 was nested in control control1, the name would be
"control1:button1". this turned out to be a really bad decision. for several reasons.

1) ":" are not allowed in the id attribute, so they are removed and replaced with _
2) while non w3c compliant browser allowed the ":" in name, it lead to
scripting errors, because the name was not valid javascript name, so could
not be used in the common syntax:

document.forms[0].myFieldname

3) IE has a bug which causes an alert if a ":" is present in a url under
https. this caused problems with postback:
javascript:_doPostBack('control1:control2')
would cause an alert on an https page, so now __dopostback() replaces the ":" with $.
javascript:_doPostBack('control1$control2')

4) the name attribute is the name the backend uses to match up the posted
data with the controls. the name still uses ":" even though some proxy
servers will strip it, and thus are not compatiable with .net
now:

ClientID is the name of the control on the server side, it does not have to be unique. Two parent controls can controls of the same name, otherwise
writing composite controls would be hard.

UniqueID is the what the rendered html attribute id will be. this is what
your javascript should use and will contain the any parent container names.
in your web control you want to:

TextBox t=new TextBox;
t.ID = "X";

Control.Attributes["OnClick"] ="javascript:document.getElementById('"
+ this.FindControl("X").UniqueID + // get the unique id of my
child control X
+ "').value='test';";
assuming the webcontrol's id is TextBox1 (and is a div), when rendered it
looks like:

<div id="TextBox1"
OnClick="javascript:document.getElementById('TextB ox1_X').value='test';" >
<input type=text id="TextBox1_X" name="TextBox1:X">
</div>

-- bruce (sqlwork.com)





"kw" <el*****************@hotmail.com> wrote in message
news:uB**************@TK2MSFTNGP11.phx.gbl...
Guess what...it still doesn't work for me (I can see how this will work in your example). And here's the funny thing: after the controls are added

and
ClientID is known (even in OnPreRender)...the final HTML looks like this:
MyControl1_MyControl1__ctl16

instead of (from tb.ClientID)

_MyControl1__ctl16

I don't understand why ASP.NET is modifying the ID after OnPreRender.

I'm being an idiot..get the ClientId AFTER you add it to the control
collection...
Dim t As New TextBox
string firstClientId = t.clientId
plc.Controls.Add(t)
string secondClientId = t.clientId
(that's a c#/vb.net mix :) ) anyways...secondClientId won't be equal to firstClientId : ) when you add it to a control, the parent'd id gets
appended, which is what you are missing

Karl



Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: news.microsoft.com | last post by:
I am using client script to handle some XML stuff with MS XMLDOM But I ran into a problem in selecting the elements using selectNodes method The XML looks like: <root> ...........................
0
by: Joel Cade | last post by:
I'm using a repeater to show the rows in a data table. I'd like the have a link button beside each row that removes the record, firing a client script function to prompt the user for confirmation,...
5
by: Chung | last post by:
Hi All, Can anyone tell me how can I call a client script (javascript) from Code behind server code (c#)? Chung
0
by: Pete Beech | last post by:
Hi, We have an ASP.NET 1.1 webapp, deployed on an integration and a production server - exactly the same code. One of the pages has client side validation script, using the validation...
4
by: Don Parker | last post by:
Does the use of server side controls eg <asp:textbox> preclude the access to that control by client script? If not, how do you reference those elements? *** Sent via Developersdex...
1
by: Julius Fenata | last post by:
Hi all, How to synchronize Label1.text between: document.getElementById("Label1").innerText (at Client Script) & Label1.text (Code Behind). I mean, I have assign new value to Label1 in Client...
2
by: ibiza | last post by:
Hi all, I have a quite big webform that has about ~15 validators. I found, after having looked at the generated HTML source, that the filesize is pretty big : about 65KB. Then, I added to every...
6
by: Velislav | last post by:
Hi, I have a client script block, which is registered in my Page_Load. However a button may result in the need to change the script which I've registered. Obviously the OnClick event occurs...
5
by: Bjorn Sagbakken | last post by:
Hello I have just migrated from VS 2003 to VS 2005, and .NET framework 1.1 to 2.0 I am at the end of debugging and fixing stuff. Now there is one error I just cannot find a solution to: On...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.