Drop Down List Example
DropDownList.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DropDownList.aspx.cs"
Inherits="Latest.DropDownList" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<b>Please Select your Department:</b>
</td>
<td>
<asp:DropDownList ID="ddlDepartment" runat="server">
<Items>
<asp:ListItem Text="Select Department" Value="-
1"></asp:ListItem>
<asp:ListItem Text="Information
Service"></asp:ListItem>
<asp:ListItem Text="Information
Technology"></asp:ListItem>
<asp:ListItem Text="Software
Engineering"></asp:ListItem>
</Items>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="Please Select Your Department!"
ForeColor="Red" ControlToValidate="ddlDepartment" InitialValue="-
1"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnShow" runat="server" Text="Show"
OnClick="btnShow_Click" />
</td>
</tr>
<tr>
<td>
<b>You have Selected:</b>
</td>
<td>
<asp:Label ID="lblStatus" runat="server" ForeColor="Red"
Font-Bold="true"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
DropDownList.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Latest
{
public partial class DropDownList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
protected void btnShow_Click(object sender, EventArgs e)
{
lblStatus.Text = ddlDepartment.Text;
}
}
}
Note: when you set dropdownlist autoPostBack= true
remove button from DropDownList.aspx and then type the following codes in
DropDownList.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Latest
{
public partial class DropDownList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
lblStatus.Text = ddlDepartment.Text;
}
}
}