

Navigator : Home > Tutorials > Controls Tutorials > ...
How to use HtmlSelect Control in ASP.NET 2.0 (VB)
To use HtmlSelect control is very easy and helpful. This tutorial will show you how to use the HtmlSelect control in ASP.NET and VB.NET 2.0.
Use the HtmlSelect control to create a selection box.
Specify item listings in the control by placing HTML <option> elements between the opening and closing <select> tags.
The following code example demonstrates how to create an HtmlSelect control by binding the control to a data source.
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
First, you will need to import the System.Web.UI.HtmlControls namespace.
The System.Web.UI.HtmlControls namespace contains the HtmlSelect Classes that we will use to create a selection box.
| Imports System.Web.UI.WebControls |
We use the Button_Click event to do the work. Button_Click control to selection.
Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer Label1.Text = "You selected:" For i = 0 To Select1.Items.Count - 1
If Select1.Items(i).Selected Then
Label1.Text = Label1.Text & " - " & Select1.Items(i).Text End If Next i End Sub |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
HtmlSelect control by binding the control to a data source
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim ConnectString As String = "server=localhost;database=pubs;integrated security=SSPI" Dim QueryString As String = "select * from authors"
Dim myConnection As SqlConnection = New SqlConnection(ConnectString) Dim myCommand As SqlDataAdapter = New SqlDataAdapter(QueryString, myConnection)
Dim ds As DataSet = New DataSet() myCommand.Fill(ds, "Authors")
Select1.DataSource = ds Select1.DataTextField = "au_fname" Select1.DataValueField = "au_fname" Select1.DataBind() End If End Sub |
The HtmlSelect.aspx page looks something like this:
<form id="form1" runat="server"> <fieldset> <legend>HtmlSelect Example</legend> <div> Select items from the list. <br/> Use the Ctrl or Shift key to select multiple items. <br/> <select id="Select1" multiple="true" runat="server"/> <br/> <button id="Button1" onserverclick="Button_Click" runat="server"> Submit </button> <br/> <asp:Label id="Label1" runat="server"/> </div></fieldset> </form> |
The flow for the code behind page is as follows:
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
Imports System Imports System.Data Imports System.Data.SqlClient
Partial Class HtmlSelect
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
Dim ConnectString As String = "server=localhost;database=pubs;integrated security=SSPI" Dim QueryString As String = "select * from authors"
Dim myConnection As SqlConnection = New SqlConnection(ConnectString) Dim myCommand As SqlDataAdapter = New SqlDataAdapter(QueryString, myConnection)
Dim ds As DataSet = New DataSet() myCommand.Fill(ds, "Authors")
Select1.DataSource = ds Select1.DataTextField = "au_fname" Select1.DataValueField = "au_fname" Select1.DataBind() End If End Sub Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer Label1.Text = "You selected:" For i = 0 To Select1.Items.Count - 1
If Select1.Items(i).Selected Then
Label1.Text = Label1.Text & " - " & Select1.Items(i).Text End If Next i End Sub End Class |
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!