Skip to content Skip to sidebar Skip to footer

Reference Asp.net Control By Id In Javascript?

When ASP.NET controls are rendered their ids sometimes change, like if they are in a naming container. Button1 may actually have an id of ctl00_ContentMain_Button1 when it is rende

Solution 1:

You can change to ClienIDMode property of the control to 'Static' that will result the same ID that you give the control in the .NET code.

<asp:TextBoxID="TextBox1"ClientIDMode="Static"runat="server"></asp:TextBox>

will result:

<input name="ctl00$MainContent$TextBox1"type="text"id="TextBox1"> 

so you have the same ID.

Solution 2:

Couple of thoughts on this:

1) I've had a lot of luck getting elements by css class instead of id because asp.net ids are not reliable as you stated. I use this function and it performs reasonably well:

functiongetElementsByClass(searchClass,node,tag) {
 var classElements = newArray();
 if ( node == null )
    {
        node = document;
    }

 if ( tag == null )
    {
        tag = '*';
    }

 var els = node.getElementsByTagName(tag);
 var elsLen = els.length;
 var pattern = newRegExp("(^|\\s)"+searchClass+"(\\s|$)");

 for (i = 0, j = 0; i < elsLen; i++) 
    {
        if ( pattern.test(els[i].className) ) 
            {
                classElements[j] = els[i];
                j++;
            }
      }
 return classElements;
}

2) jQuery helps here alot. Using jQuery you can reliably get elements where the id ends with a certain string. While this is not "the" reason to use jQuery it's definitely a plus.

3) This will be fixed in asp.net 4.0 so hang in there :-) http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx

Solution 3:

I don't think there's a single "best practice" for doing this. There's plenty of different pretty good practices. Here's ours:

Every control which has client-side functionality renders a script block inline, directly below the markup for the control:

<spanid="something_crazy_long">
    control markup
</span><scripttype="text/javascript">newCustomControl('something_crazy_long');</script>

Each control has an accompanying JS like:

varCustomControl = function(id) {
    this.control = document.getElementByID(id);
    this.init();
};

CustomControl.prototype.init = function() {
    //do stuff to wire up relevant events
};

In the codebehind, we do something like:

classCustomControl : Controloverride void Render(HtmlTextWriter writer)
{
    writer.WriteBeginTag("span");
    writer.WriteAttribute("id", this.ClientID);
    writer.Write(HtmlTextWriter.TagRightChar);
    //write control markup
    writer.WriteEndTag("span");
    writer.WriteBeginTag("script");
    writer.WriteAttribute("type", "text/javascript");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Write(
        string.Format("new CustomControl('{0}');", this.ClientID)
    );
    writer.WriteEndTag("script");
}

Solution 4:

I do something similar to Rex M except to avoid multiple script tags I use a function in my page base class to register the controls I am going to use clientside, then spit them out to html inside 1 script tag.

You could even subclass your controls to automatically register with the function or use a boolean property to set whether you are going to use them clientside.

Solution 5:

Oh, and I also found this, in case anyone else is having this problem.

Use a custom jQuery selector for asp.net controls: http://john-sheehan.com/blog/custom-jquery-selector-for-aspnet-webforms/

Post a Comment for "Reference Asp.net Control By Id In Javascript?"