How to bind DropDownList using XmlDataSource in ASP.NET

September 10, 2013 0 Comments

The XmlDataSource Control

The XmlDataSource control allows a data bound control to bind the data from a XML document. This control also supports XPath expressions that allows to return only certain nodes from the XML document.

Public Properties of XmlDataSource class

DataFile : Specifies the file name of an XML file that the data source binds to.

Advantage of XML File  

  • Its a Text file.
  • Use for carry data.
  • Build hierarchy of user defined tags. 
  • Best use in transformation.

The DropDownList Control

The DropDownList control displays the list of data as a drop-down list from which you can make a single selection. The DropDownList control exists within the System.Web.UI.WebControls namespace. You cannot select multiple items in this control because when you make a selection from the list, the list closes automatically.
The DropDownList control has no non-inherited methods or events. This class inherited the ListControl class.

Public Properties of DropDownList Class

SelectedIndex : Obtains or sets the index of the selected item in the control.

Application of DropDownList Control

  • In Registration page where you can select your country in given DropDownList.
  • In management project where you can select single option in given options.
Source :

Lets take simple example

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        Label1.Text = "Your Selected Url is <br/>" + DropDownList1.SelectedValue;

<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 bind DropdownList using XmlDataSource 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