Selecting items in usercontrol using JavaScript

I have a web form containing usercontrol and I would like to have access to the html elements in usercontrol from the form page using javascript.

I tried the following:

document.getElementById('<%= usercontrol.clientid %>')

but it returned null.

I looked around with firebug and found that tags in usercontrol are rendered using clients like usercontrolid_myelement. I assume something like this might work:

document.getElementById('<%= usercontrol.clientid %>'+'_myelement')

Is there a better way to do this?

+3
source share
3 answers

I saw your question with my psychic abilities!

Your problem is that your server server on the main page cannot access ASP.net elements of your user control.

, ClientID , , usercontrol. ClientID Javascript, .

0

, , javascript , html .

null

<head>
  <script type="text/javascript">
    alert(document.getElementById('main'));
  </script>
</head>
<body>
  <div id="main">

  </div>
</body>

<head>
  function foo(){alert(document.getElementById('main'));}
</head>
<body onload="foo();">
  <div id="main">

  </div>
</body>
0

If you use .net 4, you can stop the identifiers being created from that strange format. Just add this property asp.net ClientIDMode = "Static" for example

This should facilitate access to dom from javascript.

0
source

All Articles