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

Posted on 2007-07-25
Medium Priority
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
  • 2

Author Comment

ID: 19571509
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

ID: 19571550
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

amit_g earned 1000 total points
ID: 19572155
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

Zvonko earned 1000 total points
ID: 19574327
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);


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

807 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