Javascript function to Determine ClientID of rendered ASP.NET control.

Posted on 2007-07-25
Last Modified: 2011-10-03
My problem:
 I have a control in ASP.NET Named "img_Article_Pic" and when it is rendered in the page it is rendered as "ctl00_ContentPlaceHolder1_img_Article_Pic".  I need to create a javascript function to search and see if the control exists.  

I'm assuming that the "ct100_ContentPlaceHolder1" part of the clientID may change if I move my page to another server, so I'd like to write code that can find the control regardless of what .NET tacks onto the front.  Can anyone help?  :)

P.S. I know on the server side you can use the .ClientID to get the clientID, but I need a purely client-side solution because I'm working with the AJAX animation extender and I need to test for the existence of a control before I try to animate it.

P.S.S.  I've seen this done in the past using a regular express in javascript, but I'm not good at regex and I can't seem to find the code example anymore.  
Question by:Shiseiryu1

    Author Comment

    I just did find this nice little tidbit that you can put in the Page Load to swap out the ID with the rendered ControlID

          'Swap Out the Image Control ID with the Rendered ClientID
            AnimationExtender1.Animations = AnimationExtender1.Animations.Replace("img_Article_Pic", img_Article_Pic.ClientID)

    However, I'd still be interested in a Javascript solution if possible.  :)

    Author Comment

    Actually, the code above helps for one problem that I have, but not my main problem.  I'd really appreciate any help you all could provide with a javascript function!  :)

    Thanks in advance.
    LVL 58

    Accepted Solution

    You can do this in server side...

        Dim csname As String = "MyScript"
        Dim cstype As Type = Me.GetType()
        If (Not Page.ClientScript.IsStartupScriptRegistered(cstype, csname)) Then
          Dim cstext As String = "var global_img_Article_Pic_ClientID = '" & img_Article_Pic.ClientID & "';"
          Page.ClientScript.RegisterStartupScript(cstype, csname, cstext, True)
        End If

    and then in Javascript you would have global_img_Article_Pic_ClientID as a global variable that would contain the clientID of img_Article_Pic control.
    LVL 63

    Assisted Solution

    If your control produces a tag of type image <img>, then you can do it like this:

    window.onload = function(){
      var img = document.images;
      for(var i=0;i<img.length;i++){
          alert("The ID is: "+img[i].id);


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now