ASP.NET - javascript for getting dropdown selected value

Asked By v k on 27-Jun-12 01:20 AM
I have a dropdownlist bounded from database.While loading the dropdown list for the first time , i have to display the selected value of the dropdownlist in a label.And when the user selects any item from the dropdownlist , the selected value should get updated in the label as per the selection. I want to know that can we use javascript for this.I am new to javascript.can any give me any function for this and tell me how to call this in the selected insex change in javascript?

Thanks in advance
Jitendra Faye replied to v k on 27-Jun-12 01:25 AM
For this try this jquery code-

var value= $("#drolist1 option:selected").text();

Now you can assign this value to Label,

Like this-

document.getElemtentById("label1").value= value;


TRy this and let me know.
v k replied to Jitendra Faye on 27-Jun-12 01:27 AM
Thanks for quick reply.As I am new to java script can you please tell how to implement this?do i have to write function?how can i call it?
Jitendra Faye replied to v k on 27-Jun-12 01:41 AM
Ok, I am giving complete code here-

.aspx code-

<script type="text/javascript">
      function show() {
        var drp = document.getElementById('<%=DropDownList3.ClientID%>');
        document.getElementById('<%=Label1.ClientID%>').innerHTML = drp.value;
        alert(drp.value);
        return false;
      }

 </script>


<asp:DropDownList ID="DropDownList3" runat="server">
        <asp:ListItem>a</asp:ListItem>
        <asp:ListItem>b</asp:ListItem>
        <asp:ListItem>c</asp:ListItem>
      </asp:DropDownList>
<asp:Label ID="Label1" runat="server"></asp:Label>


.cs code-

protected void Page_Load(object sender, EventArgs e)
    {
    DropDownList3.Attributes.Add("onchange", "return show();");  
   }

Try this and let me know.

v k replied to Jitendra Faye on 27-Jun-12 01:56 AM
That' s work perfectly.Thanks a lot Vickey F
Lalit M replied to v k on 27-Jun-12 02:17 AM
Set autopostback to true and in the event handler, you have to set

Label2.Text=dropdownlst2.SelectedItem.Text to update the value to the selected item.
Jitendra Faye replied to v k on 27-Jun-12 02:34 AM
You always welcome.