Drop Down List In ASP.NET Web Forms | Web Forms Tutorial | ASP.NET Tutorial
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DropDownList
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//"!IsPostBack" means page load at first time
if (!IsPostBack)
{
ListItem L1 = new ListItem("Ahmedabad", "1");
ListItem L2 = new ListItem("Mumbai", "2");
ListItem L3 = new ListItem("Surat", "3");
ListItem L4 = new ListItem("Rajkot", "4");
//Adding Above 4 Items to DropDownList at Runtime as shown below :
DropDownList1.Items.Add(L1);
DropDownList1.Items.Add(L2);
DropDownList1.Items.Add(L3);
DropDownList1.Items.Add(L4);
DropDownList1.Items[1].Selected = true;
}
}
}
}
Above File is DropDownList\WebForm1.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DropDownList.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" Height="35px" Width="176px">
<%--<asp:ListItem Value="1" Text="Ahmedabad"></asp:ListItem>
<asp:ListItem Value="2" Text="Mumbai"></asp:ListItem>
<asp:ListItem Value="3" Text="Surat"></asp:ListItem>
<asp:ListItem Value="4" Text="Rajkot"></asp:ListItem>--%>
</asp:DropDownList>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
</div>
</form>
</body>
</html>
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment