Access DropDownList item Text, Value And Index In ASP.NET Web Forms

 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" property is mostly used with page load

            if (!IsPostBack)

            {

                //DropDownList1.SelectedIndex = 1;

                DropDownList1.SelectedValue = "1";

            }


            //"!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;

            //}

        }


        protected void Button1_Click(object sender, EventArgs e)

        {

            if (DropDownList1.SelectedValue == "-1")

            {

                Response.Write("Please select a city");

            }

            else

            {

                Response.Write("Selected Item Text is : " + DropDownList1.SelectedItem.Text + "<br>");

                Response.Write("Selected Item Value is : " + DropDownList1.SelectedItem.Value + "<br>");

                Response.Write("Selected Item Value is : " + DropDownList1.SelectedValue + "<br>");

                Response.Write("Selected Item Index is : " + DropDownList1.SelectedIndex + "<br>");

            }

        }

    }

}

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="Select City"></asp:ListItem>

                <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" OnClick="Button1_Click" />

        </div>

    </form>

</body>

</html>

Above File is DropDownList\WebForm1.aspx











Comments

Popular posts from this blog

AJAX Method Of jQuery AJAX In ASP.NET Web Forms | Learn ASP.NET Web Forms

List Box In ASP.NET Web Forms | ASP.NET ListBox | Web Forms Tutorial | ASP.NET

Display Auto Increment Value In Text Field After Insert To Database ASP.NET