How to access Text property , Value ,Index from DropDownList in ASP.NET

September 14, 2013 0 Comments

Top article


Text Property : you can access or set the item text which is in DropDownList. If you want to access the text of the DropDownList then simply you can use
String var=DropDownList1.items[i].Text; (this is correct)
Here items[i] is directly denote to collection so you can not use 
String var=DropDownList1.items.Text;  ( This is incorrect)
If you want to add items to the DropDownList , simply you can use add method 

Lets take an example to access index,Text and value of the selected item in DropDownList

In above mentioned top article we learn how to bind DropDownList with XmlDataSource in ASP.NET. Also this example show that how to access the text of DropDownList.
<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        Label1.Text += "Your Selected value is <br/>" + DropDownList1.SelectedValue;
        Label1.Text += "<br/> Your Selected Text is <br/>" + DropDownList1.SelectedItem.Text;
        Label1.Text += "<br/> Your Selected Item Index is <br/>" + DropDownList1.SelectedIndex.ToString();

    protected void Page_Load(object sender, EventArgs e)


<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="XmlDataSource1" DataTextField="Text" DataValueField="url" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile.xml"></asp:XmlDataSource>
        <asp:Label ID="Label1" runat="server"></asp:Label>

How to access Text property , Value ,Index from DropDownList in ASP.NET

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google