• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 906
  • Last Modified:

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

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.  
  • 2
2 Solutions
Shiseiryu1Author Commented:
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.  :)
Shiseiryu1Author Commented:
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.
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.
ZvonkoSystems architectCommented:
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);

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now