Home All Groups Group Topic Archive Search About
Author
18 Apr 2006 4:46 PM
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

Author
19 Apr 2006 5:32 PM
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

Show quoteHide quote
"Adam  Plocher" wrote:

> 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
>
>
Author
19 Apr 2006 11:20 PM
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?
Author
20 Apr 2006 8:01 AM
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>");
Author
20 Apr 2006 5:00 PM
Adam Plocher
Thanks Guffa, I'll play around with these and see what I come up with.