Home All Groups Group Topic Archive Search About

issue: accessing value of dropdownlist in a user control

Author
13 Apr 2006 7:52 PM
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?


--
E of the City

Author
14 Apr 2006 4:12 PM
Phillip Williams
ASP.NET renders its server controls using a unique naming strategy: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/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)

Show quoteHide quote
"E" wrote:

> 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?
>
>
> --
> E of the City
Are all your drivers up to date? click for free checkup

Author
17 Apr 2006 5:53 PM
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.

--
E of the City


Show quoteHide quote
"E" wrote:

> 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?
>
>
> --
> E of the City

Bookmark and Share

Post Thread options