In this article i show you how to declare dropdownlist different ways in asp.net
First we can declare in source code
1st way:-
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Select State" Value="-1"></asp:ListItem>
<asp:ListItem Text="AP" Value="1"></asp:ListItem>
<asp:ListItem Text="UP" Value="2"></asp:ListItem>
<asp:ListItem Text="TS" Value="3"></asp:ListItem>
<asp:ListItem Text="MP" Value="4"></asp:ListItem>
<asp:ListItem Text="Tamilnadu" Value="5"></asp:ListItem>
</asp:DropDownList>
2nd way:-
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="-1">Select State</asp:ListItem>
<asp:ListItem Value="1">AP</asp:ListItem>
<asp:ListItem Value="2">UP</asp:ListItem>
<asp:ListItem Value="3">TS</asp:ListItem>
<asp:ListItem Value="4">MP</asp:ListItem>
<asp:ListItem Value="5">Tamilnadu</asp:ListItem>
</asp:DropDownList>
we can declare code behind also under page load event
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList1.Items.Add("AP");
DropDownList1.Items.Add("UP");
DropDownList1.Items.Add("MP");
DropDownList1.Items.Add("TS");
DropDownList1.Items.Add("Tamilnadu");
//insert an item as string at zero index
DropDownList1.Items.Insert(0,"Select State");
//or
ListItem li1 = new ListItem("AP", "1");
ListItem li2=new ListItem("UP","1");
ListItem li3=new ListItem("MP","1");
ListItem li4=new ListItem("TS","1");
ListItem li5=new ListItem("Tamilnadu","1");
DropDownList1.Items.Add(li1);
DropDownList1.Items.Add(li2);
DropDownList1.Items.Add(li3);
DropDownList1.Items.Add(li4);
DropDownList1.Items.Add(li5);
//insert an item as string at zero index
ListItem li6 = new ListItem("Select State", "0");
DropDownList1.Items.Insert(0, li6);
//or
DropDownList1.Items.Add(new ListItem("AP", "1"));
DropDownList1.Items.Add(new ListItem("UP", "2"));
DropDownList1.Items.Add(new ListItem("MP", "3"));
DropDownList1.Items.Add(new ListItem("TS", "4"));
DropDownList1.Items.Add(new ListItem("Tamilnadu", "5"));
//insert an item as string at zero index
DropDownList1.Items.Insert(0, new ListItem("Select State", "0"));
}
}
First we can declare in source code
1st way:-
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Select State" Value="-1"></asp:ListItem>
<asp:ListItem Text="AP" Value="1"></asp:ListItem>
<asp:ListItem Text="UP" Value="2"></asp:ListItem>
<asp:ListItem Text="TS" Value="3"></asp:ListItem>
<asp:ListItem Text="MP" Value="4"></asp:ListItem>
<asp:ListItem Text="Tamilnadu" Value="5"></asp:ListItem>
</asp:DropDownList>
2nd way:-
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="-1">Select State</asp:ListItem>
<asp:ListItem Value="1">AP</asp:ListItem>
<asp:ListItem Value="2">UP</asp:ListItem>
<asp:ListItem Value="3">TS</asp:ListItem>
<asp:ListItem Value="4">MP</asp:ListItem>
<asp:ListItem Value="5">Tamilnadu</asp:ListItem>
</asp:DropDownList>
we can declare code behind also under page load event
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList1.Items.Add("AP");
DropDownList1.Items.Add("UP");
DropDownList1.Items.Add("MP");
DropDownList1.Items.Add("TS");
DropDownList1.Items.Add("Tamilnadu");
//insert an item as string at zero index
DropDownList1.Items.Insert(0,"Select State");
//or
ListItem li1 = new ListItem("AP", "1");
ListItem li2=new ListItem("UP","1");
ListItem li3=new ListItem("MP","1");
ListItem li4=new ListItem("TS","1");
ListItem li5=new ListItem("Tamilnadu","1");
DropDownList1.Items.Add(li1);
DropDownList1.Items.Add(li2);
DropDownList1.Items.Add(li3);
DropDownList1.Items.Add(li4);
DropDownList1.Items.Add(li5);
//insert an item as string at zero index
ListItem li6 = new ListItem("Select State", "0");
DropDownList1.Items.Insert(0, li6);
//or
DropDownList1.Items.Add(new ListItem("AP", "1"));
DropDownList1.Items.Add(new ListItem("UP", "2"));
DropDownList1.Items.Add(new ListItem("MP", "3"));
DropDownList1.Items.Add(new ListItem("TS", "4"));
DropDownList1.Items.Add(new ListItem("Tamilnadu", "5"));
//insert an item as string at zero index
DropDownList1.Items.Insert(0, new ListItem("Select State", "0"));
}
}
No comments:
Post a Comment