Home > Articles > ASP.NET Articles > How to bind dropdown list with XML file in asp.net?

How to bind dropdown list with XML file in asp.net?

by Thamilselvanj   on Aug 29, 2011   Category: ASP.NET  | Level: Beginner  |  Views: 807    |  Points: 100   
Like this article? Bookmark and Share:
In this article I’m going to explain how to bind dropdown list with xml file. Binding dropdown list with values from database is common requirement; sometime we may have a requirement like binding the xml file with dropdown list




Introduction


This is easy way to bind dropdown list to xml file, let us create the sample department xml file as shown below

<Company>

  <department>

    <deptId>1</deptId>

    <deptName>Finance</deptName>

  </department>

  <department>

    <deptId>2</deptId>

    <deptName>Human Resource</deptName>

  </department>

  <department>

    <deptId>3</deptId>

    <deptName>Information Technology</deptName>

  </department>

  <department>

    <deptId>4</deptId>

    <deptName>System Network</deptName>

  </department>

  <department>

    <deptId>5</deptId>

    <deptName>Customer Support</deptName>

  </department>

</Company>

Store the above xml file in the application root and read the department.xml file and store into the dataset using ReadXml() method which is available in the dataset and then finally bind with the dropdown list. Design the page as shown below
 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm6.aspx.cs" Inherits="WebApplication1.WebForm6" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Sample binding xml file into dropdown list</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        Department : <asp:DropDownList ID="DrpDepartment" AutoPostBack="true"

            runat="server" onselectedindexchanged="DrpDepartment_SelectedIndexChanged">

        </asp:DropDownList>

    </div>

    </form>

    <p>

        Selected Department :

        <asp:Label ID="lblDept" runat="server" Text="None"></asp:Label>

    </p>

</body>

</html>

 

Write the below code in the page load event and call the LoadDropdownList method and assign the dropdown list DataTextField and DataValueField properties. The LoadDropdownList() method will execute only once when the page loads because of verifying through IsPostBack property and it would not call each page loads/post backs 

protected void Page_Load(object sender, EventArgs e)

        {

            try

            {

                if (!IsPostBack)

                {

                    LoadDropdownList();

                }

            }

            catch (Exception ex)

            {

                Response.Write(ex.Message);

            }

        }

        private void LoadDropdownList()

        {

            try

            {

                DataSet dsDept = new DataSet();

                dsDept.ReadXml(Server.MapPath("Departments.xml"));

                DrpDepartment.DataSource = dsDept;

                DrpDepartment.DataTextField = "deptName";

                DrpDepartment.DataValueField = "deptId";

                DrpDepartment.DataBind();

                DrpDepartment.Items.Insert(0, new ListItem("-- Select --", "0"));

            }

            catch(Exception ex)

            {

                throw ex;

            }

        }

 

To retrieve the dropdown list selected value, create the SelectedIndexChanged event and the write the below code. Dropdown list autopostback property value must be true so that post back will be happen when the selection of dropdown control.

protected void DrpDepartment_SelectedIndexChanged(object sender, EventArgs e)

        {

            try

            {

                lblDept.Text = DrpDepartment.SelectedItem.Text + "-" + DrpDepartment.SelectedValue;

            }

            catch (Exception ex)

            {

                Response.Write(ex.Message);

            }

        }

 

Conclusion

In this aricle, we know how to bind dropdown list with xml file in a simple way. 

User Responses | Post Article
Comment posted by Zung on 2/22/2012 6:29:34 AM | Points : 10
yes
Comment posted by Zung on 2/22/2012 6:29:52 AM | Points : 10
protected void DrpDepartment_SelectedIndexChanged(object sender, EventArgs e)

{

try

{

lblDept.Text = DrpDepartment.SelectedItem.Text + "-" + DrpDepartment.SelectedValue;

}

catch (Exception ex)

{

Response.Write(ex.Message);

}

}
  Most viewed Articles
* Programmatically Update the UpdatePanel using ASP.NET (828) by Shiva on May 15 2011 9:16AM
* How to bind dropdown list with XML file in asp.net? (807) by Thamilselvanj on Aug 29 2011 12:23AM
* What is windows service? How to create windows service using .Net? (653) by Thamilselvanj on Nov 14 2010 9:48PM
* What is XML? (547) by Narain_Siddharth on Dec 19 2010 2:24AM
* Static classes and static members in C# (451) by Spidy on Jan 10 2011 12:28PM
 Submit feedback about this article
Please sign in to post feedback