views:

58

answers:

4

Hi,

I've inherited a project with some nasty JavaScript that depends on hard coded object ids.
i.e. There are lots of places where it does things like this

var magazine = document.getElementById('repModuleDisplay__ctl3_chkCats_0');

When the page renders in my UAT environment, the HTML looks like this, and everything works OK.

<input id="repModuleDisplay__ctl3_chkCats_0" 
    type="checkbox" name="repModuleDisplay:_ctl3:chkCats:0"  
    ... etc

However, when I put it on my Production environment, the HTML is suddenly rending like this:

<input id="repModuleDisplay_ctl03_chkCats_0" 
    type="checkbox" name="repModuleDisplay$ctl03$chkCats$0" 
    ... etc

The difference in ids means that the JavaScript can't find the Element, and fails.

In an ideal world, I'd scrap the buggy JavaScript and do it again properly, but for a quick fix, I'd like to know what is causing the difference in rendering between the two environments.

Does anyone have any ideas?

Thanks, Neil

+2  A: 

Use .NET4 and set the ClientIDMode property on your controls that you want to use with js.

EG;

  <asp:TextBox ID="txtComment" runat="server" CssClass="myClass"
    ClientIDMode="Static" MaxLength="1024"></asp:TextBox>
GordonB
Sadly not an option... This is a nasty project with some pages in .Net 3.5, some in .Net 2, and some in .Net 1.1...Guess which version is causing the problem?? .Net 1.1
NeilD
I'm not sure that updating the entire development, test and production environments to use .NET4 would qualify as "a quick fix"!
LukeH
You got me there... but something to bear in mind :)
GordonB
@NeilD - you might want to use this an example problem, bite the bullet and upgrade the 1.1 stuff at least. This must be a nightmare to maintain.
Paddy
@Paddy - Oh, boy... The .Net 1.1 parts are only the tip of the iceberg as far as maintainability problems go. This is a shocker of a project!
NeilD
+4  A: 

You must use clientID for asp.net control.

'<%=mycontrol.ClientID %>'
Brij
+7  A: 

The quickest and easiest fix I can think of -- and it's probably not particularly quick or easy if the code is all over the place -- is to use the actual ClientID property of the control instead of trying to guess what the generated id might be:

// i've assumed that the control is named chkCats
var magazine = document.getElementById('<%= chkCats.ClientID %>');
LukeH
Thanks Luke... The only potential hiccup is that there are LOADS of chkCat checkboxes, and the JavaScript ought to fire only when one specific one is clicked (I know... I know...). I'll give it a go, and see if I can make it work.
NeilD
@NeilD: Another possibility (especially if the checkboxes are contained in some sort of container like a repeater) might be to use JavaScript - or maybe something like jQuery - to find the appropriate checkbox on the client-side at runtime.
LukeH
+1  A: 

This link gives you a brief description of how ASP.NET creates the ids, and all the other posters have recommended the right solution of using the control's ClientID.

If you want to steer clear of the ASP style tags, then another option is to emit some js to your page during the PreRender:

StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Format("var myTextBox = document.getElementById('{0}');", MyTextBox.ClientID));
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "myElementDefinitions"))
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myElementDefinitions", sb.ToString());

and then in your javascript code you just refer to it with the name myTextBox. This option may not suit you, depends upon your code and how often you are using ASP tags to reference the same control id.

slugster