DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 How to use HtmlSelect Control in ASP.NET 2.0 (C#)

To use HtmlSelect control is very easy and helpful. This tutorial will show you how to use the HtmlSelect control in ASP.NET 2.0 and C#. 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.

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.

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.

using System.Web.UI.WebControls;

We use the Button_Click event to do the work. Button_Click  control to  selection.

protected void Button_Click (Object sender, EventArgs e)
{
Label1.Text = "You selected:";

for (int i=0; i<=Select1.Items.Count - 1; i++)
{
if (Select1.Items[i].Selected)
Label1.Text += "
   - " + Select1.Items[i].Text;
}
}

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 void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string ConnectString = "server=localhost;database=pubs;integrated security=SSPI";
string QueryString = "select * from authors";

SqlConnection myConnection = new SqlConnection(ConnectString);
SqlDataAdapter myCommand = new SqlDataAdapter(QueryString, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "Authors");

Select1.DataSource = ds;
Select1.DataTextField = "au_fname";
Select1.DataValueField = "au_fname";
Select1.DataBind();
}
}

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:

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


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class HtmlSelect : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string ConnectString = "server=localhost;database=pubs;integrated security=SSPI";
string QueryString = "select * from authors";

SqlConnection myConnection = new SqlConnection(ConnectString);
SqlDataAdapter myCommand = new SqlDataAdapter(QueryString, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "Authors");

Select1.DataSource = ds;
Select1.DataTextField = "au_fname";
Select1.DataValueField = "au_fname";
Select1.DataBind();
}
}

protected void Button_Click (Object sender, EventArgs e)
{
Label1.Text = "You selected:";

for (int i=0; i<=Select1.Items.Count - 1; i++)
{
if (Select1.Items[i].Selected)
Label1.Text += "<br> &nbsp;&nbsp; - " + Select1.Items[i].Text;
}
}
}



Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!