Accessing HTML Control in ASCX through JavaScript

G

George Durzi

I have a JavaScript called common.js that is referenced by every ASPX in my
web application. In common.js, I have a funtion called findText (which I use
to help me navigate through drop down lists by typing the first few letters
of an item in the drop down list).

So this is how I would use it:

<input type="text" id="searchCompany" size="1" name="searchCompany"
onkeyup="findText(this, Company);">
<asp:dropdownlist ID="Company" Runat="server"></asp:dropdownlist>

All this is fine on a normal webform. But I run into trouble when my
dropdown list is inside a user control. Consider this pseudo HTML (as it
would look like after the page was rendered)

<html>
<head>
<script src='http://localhost/mywebapp/js/common.js'
language='JavaScript'></script>
</head>
<form id="frmMain" runat="server">
<McAddEditAscx User Control Rendered>
<input type="text" id="searchCompany" size="1"
name="searchCompany" onkeyup="base.findText(this, Company);">
<select name="_McAddEditAscx:Company"
id="_McAddEditAscx_Company">
<option value="something">something</option>
...
</select>
</McAddEditAscx User Control Rendered>
</form>
</html>


Notice that when the UserControl got rendered my dropdownlist with
id="Company" was rendered as _McAddEditAscx:Company

I tried fugding my searchCompany box by doing

<input type="text" id="searchCompany" size="1" name="searchCompany"
onkeyup="base.findText(this, _McAddEditAscx:Company);">

I don't that's a clean solution though.

So how do I access HTML controls that are in a user control using
JavaScript?

THANK YOU!!
 
C

Chris Jackson

You can either output the unique id of those controls in your server side
code (which will include the name decoration), or you can iterate through
the controls looking at substrings for the text that you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top