Drop List

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 vb.net
Who is Participating?
Luis PérezConnect With a Mentor Software Architect in .NetCommented:
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.
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<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

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" />

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.