KEMBAR78
ASP.NET DropDownList Example Code | PDF | Active Server Pages | Software
0% found this document useful (0 votes)
103 views4 pages

ASP.NET DropDownList Example Code

This document provides an example of using a drop down list in ASP.NET. It includes the code to create a drop down list with selectable departments, a button to display the selected department, and code behind to handle the button click event and update the label. It also notes that setting the drop down list's autopostback property to true allows removing the button, and handling the selection change event instead to immediately update the label.

Uploaded by

Abdul waris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views4 pages

ASP.NET DropDownList Example Code

This document provides an example of using a drop down list in ASP.NET. It includes the code to create a drop down list with selectable departments, a button to display the selected department, and code behind to handle the button click event and update the label. It also notes that setting the drop down list's autopostback property to true allows removing the button, and handling the selection change event instead to immediately update the label.

Uploaded by

Abdul waris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like