Friday, September 3, 2010

DropDownList Server Control

The DropDownList server control enables you to place an HTML select box on your Web page and program against it. It's deal when you have a large collection of items from which you want the user to select a single item.






Dynamically Generating a DropDownList



.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>DropDownList Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%-- First Method --%>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
<asp:ListItem>Three</asp:ListItem>
<asp:ListItem>Four</asp:ListItem>
<asp:ListItem>Five</asp:ListItem>
</asp:DropDownList>

<%-- Second Method --%>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Text="" Value="0" Selected="True"></asp:ListItem>
<asp:ListItem Text="Red" Value="Red"></asp:ListItem>
<asp:ListItem Text="Yellow" Value="Yellow"></asp:ListItem>
<asp:ListItem Text="Orange" Value="Orange"></asp:ListItem>
<asp:ListItem Text="Blue" Value="Blue"></asp:ListItem>
<asp:ListItem Text="NoColor" Value="NoColor"></asp:ListItem>
</asp:DropDownList>


<%-- Third Method --%>

<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList>


</div>
</form>
</body>
</html>



.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

//Third Method
string[] Cars = { "Honda", "Maruthi", "Suzuki", "Benz", "BMW" };

DropDownList3.DataSource = Cars;
DropDownList3.DataBind();

}
}

No comments:

Post a Comment