Drop Down List In ASP.NET Web Forms | Web Forms Tutorial | ASP.NET

 <%@ 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" Width="150px">

                <%--<asp:ListItem Value="1" Text="Ahmedabad"></asp:ListItem>

                <asp:ListItem Value="2" Text="Mumbai"></asp:ListItem>

                <asp:ListItem Value="3" Text="Rajkot"></asp:ListItem>

                <asp:ListItem Value="4" Text="Surat"></asp:ListItem>--%>


            </asp:DropDownList>

            <br />

            <br />

            <asp:Button ID="Button1" runat="server" Text="SUBMIT"/>

        </div>

    </form>

</body>

</html>

Above File is DropDownList\WebForm1.aspx

















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)
        {
            if (!IsPostBack)
            {
                //Below ListItem values will add to DropDownList at Runtime
                ListItem L1 = new ListItem("Ahmedabad", "1");
                ListItem L2 = new ListItem("Mumbai", "2");
                ListItem L3 = new ListItem("Rajkot", "3");
                ListItem L4 = new ListItem("Surat", "4");

                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






Comments

Popular posts from this blog

Range Validator Control In ASP.NET Web forms | Form Validation | ASP.NET Web forms

Query String In ASP.NET Web Forms | State Management | ASP.NET | Web Forms

Validation Summary Control In ASP.NET Web forms | Form Validation | ASP.NET