issue: accessing value of dropdownlist in a user control

E

E

hi,

I am having trouble getting the value of a dropdownlist from javascript.

I have a form that contains a user control that has a drop downlist.

the usercontrol is declared as follows:
protected frm3H_1 Frm3H_11;

in the user control I declare the dropdownlist
protected System.Web.UI.WebControls.DropDownList lstdirectorindirectownership;


there is some javascript included in the form that is trying to access the
value of the dropdownlist.

I placed this in my usercontrol ascs file.
<tr>
<td><span class="bodytext">
<asp:dropdownlist id="lstdirectorindirectownership" runat="server"
Width="92px">
<asp:ListItem Value="1">D</asp:ListItem>
<asp:ListItem Value="0">I</asp:ListItem>
</asp:dropdownlist></span></td>
</tr>


In the javascript I am trying to get it by the following.

var selectedIndex =
document.table1.Frm3H_11_lstdirectorindirectownership.selectedIndex
alert("Selected Index is " + selectedIndex);
var selectedValue =
document.table1.Frm3H_11_lstdirectorindirectownership[selectedIndex].value;
alert("Selected Value is \'" + selectedValue + "\'");

It does not seem to work. Can anyone provide some insight to what I should
be doing?
 
P

Phillip Williams

ASP.NET renders its server controls using a unique naming strategy:
http://msdn.microsoft.com/library/d...n/html/vbconwebformscontrolidentification.asp

To be able to find a control using client-side scripting you need to locate
the element by its ClinetID. Therefore you have to use the
RegisterStartupScript to send to the browser variables containing the
ClientIDs of the objects you intend to manipulate. To see an example of such
client-side scripting of server side object, you might review the source code
of this demo from my website (which uses a TreeView but the concept is the
same when you attempt to get the clientID for the dropdonwlists)
 
E

E

I figured it out. took me a while but here is the solution.

var Index =
document.table1.Frm3H_11_lstdirectorindirectownership.options.selectedIndex;
var Value =
document.table1.Frm3H_11_lstdirectorindirectownership.options[Index].value;

The rest is ok.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top