Follow us

Header Ads

DropDownList Control in Asp.Net WebForm

 DropDownlistControl.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
           DesignTimeDropDownList:<asp:DropDownList ID="DropDownList1" runat="server" Height="16px" Width="179px">
                <asp:ListItem Value="1">Hyderabad</asp:ListItem>
                <asp:ListItem Value="2">Karachi</asp:ListItem>
                <asp:ListItem Value="3" Enabled="false">Lahor</asp:ListItem>
                <asp:ListItem Value="4" Selected="True">Isalamabad</asp:ListItem>
            </asp:DropDownList>
            <br />
            <br />
            RunTimeDropDownList:<asp:DropDownList ID="DropDownList2" runat="server" Height="29px" Width="200px">
            </asp:DropDownList>
            <br />
            <br />
            

            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
        </div>
    </form>
</body>
   //Enable Proerty is use to hide and unhide dropdown list
<p>
        &nbsp;</p>
</html>

 DropDownlistControl.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DropDownlistControl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListItem L1 = new ListItem("Hyderabad", "1");
            ListItem L2 = new ListItem("Karachi", "2");
            ListItem L3 = new ListItem("Lahore", "3");
            ListItem L4 = new ListItem("Islamabad", "4", false);
            //false is use to enable and disable dropdown list

            DropDownList2.Items.Add(L1);
            DropDownList2.Items.Add(L2);
            DropDownList2.Items.Add(L3);
            DropDownList2.Items.Add(L4);
            //It is use to View Selected Item using Index No
            DropDownList2.Items[1].Selected = true;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}
//Hyderabad is text and 1 is Value

Post a Comment

0 Comments