Control ID In Javascript

  • Thread starter Thread starter senfo
  • Start date Start date
S

senfo

I'm having problems obtaining the client-side ID in my C# code. I've
tried to access it through the ID, UniqueID and ClientID properties, and
none of them show the entire ID.

The rendered HTML is always ctl00_PageContent_PlaceHolder_.... while
it's always just the plain ID on the server side.

Any ideas?

Thank you in advance,
 
I'm having problems obtaining the client-side ID in my C# code. I've tried
to access it through the ID, UniqueID and ClientID properties, and none of
them show the entire ID.

If none of them shows the entire ID, what do they actually show...?

..ClientID should show the rendered ID of the control so that e.g. you could
reference it in client-side JavaScript with document.getElementById(...)
The rendered HTML is always ctl00_PageContent_PlaceHolder_.... while it's
always just the plain ID on the server side.

That's correct.
Any ideas?

Not until you actually specify what the problem is...
 
Mark said:
If none of them shows the entire ID, what do they actually show...?

Given the following code, each label displays the text, "SomeValue".

MyControl.ID = "SomeValue";

Label1.Text = MyControl.ID;
Label2.Text = MyControl.UniqueID;
Label3.Text = MyControl.ClientID;
.ClientID should show the rendered ID of the control so that e.g. you could
reference it in client-side JavaScript with document.getElementById(...)


That's correct.


Not until you actually specify what the problem is...

I except one of those properties to tell me what the *real* client-side
ID is, which is always ctl00_PageContent_PlaceHolder_....

This isn't good because I need it for a JavaScript function.

Example:

Image img = new Image();

img.ImageUrl = "img.png";
img.ID = "MyTest";
img.Attributes.Add("onclick", "MyFunction('" + img.ClientID + "');");

This renders as

<img id="ctl00_PageContent_PlaceHolder_MyTest" src="img.png"
onclick="MyFunction('MyTest');" />

Please note that if anything looks a little strange, it's probably
becaused I typed all of that by hand without Visual Studio.

Thank you again,
 
senfo said:
I'm having problems obtaining the client-side ID in my C# code. I've
tried to access it through the ID, UniqueID and ClientID properties, and
none of them show the entire ID.

The rendered HTML is always ctl00_PageContent_PlaceHolder_.... while
it's always just the plain ID on the server side.

Any ideas?

Thank you in advance,

By the way, it may be important to note that the problem I am
experiencing is in a custom web control that I'm writing.
 
Back
Top