views:

1487

answers:

2

If I pass a ClientID to a javascript function as a parameter without including the single quotes around it, it gets passed as a reference to the control itself which can then be used without first calling getElementByID.

I can't find this behaviour documented anywhere, is this a browser specific thing or a .net thing or what?

I am setting up the call like this in code-behind...

protected void Page_Load(object sender, EventArgs e)
     {
      Button1.Attributes.Add("onClick", string.Format("showvalue({0})",  TextBox1.ClientID));
     }

My concern is that this may not work in older versions of IE. Thanks.

A: 

If you pass it with single quotes and use getElementByID it should work in all browsers, that's the "normal" way.

I'm not sure why this method works as you say. Does it also work in Firefox?

Gunnar Steinn
+4  A: 

Add quotes around your ID value when you generate the JS code e.g: string.Format("showvalue('{0}')", TextBox1.ClientID)

Without quotes, showValue gets an instance of the global variable with your ClientID name that usually is the DOM element your control rendered.

Hristo Deshev