Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - How to use HtmlSelect Control in ASP.NET 2.0 (VB)
How to use HtmlSelect Control in ASP.NET 2.0 (VB)


ASP.NET Controls Tutorial

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.

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

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

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

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

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>

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The flow for the code behind page is as follows:

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

Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks