ID of web control changed

A

Adam Plocher

Hello, I have been developing a website locally for quite some time now
in ASP.NET 2.0/C# and all of the web controls (locally) have IDs like
this: ctl00_ContentPlaceHolder1_ddSearchType

I just uploaded it to our live site today, for the first time, and now
all of my IDs look like this:
_ctl0_ContentPlaceHolder1_ddSearchType

What gives? My javascript is now all broken because of this. Is there
a place where I can specify to use ctl00 instead of _ctl0 ? The only
way I can think to fix this is by using a literal (or something) and
have c# generate the JS on the fly using the controls UniqueID
property, which seems really stupid. It's hundreds of lines of JS.

Thanks
-Adam
 
G

Guffa

You shouldn't rely on the IDs to be in a specific format. Use the ClientID
property to get the ID to use in Javascript.

/Guffa
 
A

Adam Plocher

So should I be loading all my JS into a Literal or something? I can't
just write JS code into my aspx or into a standalone .js file?
 
G

Guffa

So should I be loading all my JS into a Literal or something? I can't
just write JS code into my aspx or into a standalone .js file?

You have several options. You can either create all of the code dynamically,
or just part of it. You can for an example make wrappers for the fields that
you need to access:

function getSomeField() {
return document.getElementById('<asp:Literal id="SomeFieldLiteral"
runat="server"/>');
}

and in code-behind:

SomeFieldLiteral.Text = SomeField.ClientId;

You can also create the wrapper entirely frfom code-behind:

Page.RegisterClientScriptBlock("GetSomeField", "<script
type=\"text/javascript\">function getSomeField() { return
document.getElementById('" + SomeField.ClientId + "'); }</script>");
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top