Drop List

Posted on 2011-10-19
Last Modified: 2012-05-12
I have a web page with a drop list that have values I want to take the value the user selects and put it in a lable using
Question by:jordanhelen
    LVL 25

    Accepted Solution

    Put this into the HEAD section of the HTML part of the page:
    <script type="text/javascript">
        function comboChange(combo) {
            document.getElementById('<%=Me.Label1.ClientID%>').innerHTML = combo.value;

    And put this into the Page_Load method:
    Me.DropDownList1.Attributes.Add("onchange", "comboChange(this)")

    Obviously, replace "Label1" and "DropDownList1" with your controls' names.

    Hope that helps.
    LVL 2

    Expert Comment

    You should do it with javascript.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="" >
    <head runat="server">
        <title>Untitled Page</title>
        <script type="text/javascript">
            function displayvalue(obj)
        <form id="form1" runat="server">
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem Text="A" Value="A"></asp:ListItem>
                <asp:ListItem Text="B" Value="B"></asp:ListItem>
                <asp:ListItem Text="C" Value="C"></asp:ListItem>
                <asp:ListItem Text="D" Value="D"></asp:ListItem>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    Open in new window

    Partial Class _Default
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                DropDownList1.Attributes.Add("onchange", "displayvalue(this)")
            End If
        End Sub
    End Class

    Open in new window

    LVL 20

    Expert Comment

    I agree you can and may want to do this with JS, but if by chance you want to do it the way you originally asked it can be done like this:

    <<see code>>
    ****ASPX PAGE****
    <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
    	<asp:ListItem value="-1" Text="Choose" />
    	<asp:ListItem value="0" Text="Zero" />
    	<asp:ListItem value="1" Text="One" />
    <asp:Label ID="lbl1" runat="server" />
    ***CODE BEHIND***
    Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
       If ddl1.SelectedValue > -1 Then
          lbl1.Text = String.Format("Text={0} AND Value={1}", ddl1.SelectedItem.Text, ddl1.SelectedItem.Value)
          lbl1.Text = String.Empty
       End If
    End Sub

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now