Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
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 (C#)
How to use HtmlSelect Control in ASP.NET 2.0 (C#)

ASP.Net 4.0 Tutorials V4
Server Intellect Cloud Hosting

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 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.

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 moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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;
}
}

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.

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>

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.

The flow for the code behind page is as follows:

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!



 
  Developer Resources







Server Intellect Rocks