Bind DropDownList using XML File.

This blog demonstrates how to bind a drop down list control using xml file.
This is little simple process but it's very useful. First of all drag and drop a Drop Down List control on page.

<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="ddlTitle" runat="server"></asp:DropDownList>
    </div>
    </form>
</body>


Add a namespace on code behind.

using System.Data;

and write few lines of code on page load event to fill drop down list.

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(Server.MapPath("Advertisements.xml"));
            DataView dataView = dataSet.Tables[0].DefaultView;

    //Note Title is a column name which you want to bind in drop down list.

            dataView.Sort = "Title";            
            ddlTitle.DataTextField = "Title";
            ddlTitle.DataValueField = "Title";

            ddlTitle.DataSource = dataView;
            ddlTitle.DataBind();
        }
    }

After compiling application you will see result like this.

img1.jpg
Ebook Download
View all
Learn
View all