S
Stephen Adam
Hi there,
I'm sure i'm missing something really simple here, all i want to do is get
the value of the selected item in a list box. Even after much fiddling about
last night I still could not get my code to work. Below is some code which
highlights my problem. All I want to do is set the lable control's text
property to the value of the selected drop down list value - in this example
i've shown the three ways i've tried.
Please help!
Thanks
Steve
VB CODE
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Protected WithEvents ddlNames As System.Web.UI.WebControls.DropDownList
Protected WithEvents lblResult As System.Web.UI.WebControls.Label
Protected WithEvents btnRun As System.Web.UI.WebControls.Button
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Run(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnRun.Click
' None of these work
lblResult.Text = ddlNames.SelectedValue
lblResult.Text = ddlNames.SelectedValue.ToString
lblResult.Text = ddlNames.SelectedItem.Text
End Sub
End Class
ASPX CODE
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
Inherits="dropdownlist.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema
content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<h1> Drop Down List Example </h1>
</br>
</br>
<aspropDownList ID="ddlName" Runat="server">
<asp:ListItem Selected="True"
Value="Bill">Bill</asp:ListItem>
<asp:ListItem Value="Jason">Jason</asp:ListItem>
<asp:ListItem Value="Tim">Tim</asp:ListItem>
</aspropDownList>
</br>
</br>
<asp:Button ID="btnRun" Runat="server" Text="Run"></asp:Button>
</br>
</br>
<asp:Label ID="lblResult" Runat="server"> </asp:Label>
</form>
</body>
</html>
I'm sure i'm missing something really simple here, all i want to do is get
the value of the selected item in a list box. Even after much fiddling about
last night I still could not get my code to work. Below is some code which
highlights my problem. All I want to do is set the lable control's text
property to the value of the selected drop down list value - in this example
i've shown the three ways i've tried.
Please help!
Thanks
Steve
VB CODE
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Protected WithEvents ddlNames As System.Web.UI.WebControls.DropDownList
Protected WithEvents lblResult As System.Web.UI.WebControls.Label
Protected WithEvents btnRun As System.Web.UI.WebControls.Button
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Run(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnRun.Click
' None of these work
lblResult.Text = ddlNames.SelectedValue
lblResult.Text = ddlNames.SelectedValue.ToString
lblResult.Text = ddlNames.SelectedItem.Text
End Sub
End Class
ASPX CODE
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
Inherits="dropdownlist.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema
content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<h1> Drop Down List Example </h1>
</br>
</br>
<aspropDownList ID="ddlName" Runat="server">
<asp:ListItem Selected="True"
Value="Bill">Bill</asp:ListItem>
<asp:ListItem Value="Jason">Jason</asp:ListItem>
<asp:ListItem Value="Tim">Tim</asp:ListItem>
</aspropDownList>
</br>
</br>
<asp:Button ID="btnRun" Runat="server" Text="Run"></asp:Button>
</br>
</br>
<asp:Label ID="lblResult" Runat="server"> </asp:Label>
</form>
</body>
</html>