How to bind asp.net dropdown list with XML file in using C Sharp and ASP.NET

Category > ASP.NET || Published on : Friday, May 1, 2015 || Views: 6371 || bind asp.net dropdown list with XML file asp.net dropdown list with example dropdown list with XML


How to bind asp.net dropdown list with XML file in using C Sharp and ASP.NET

In this article we are going to learn how to bind asp.net dropdown list with xml file. Binding dropdown list with values from database is common requirement; in some cases, we may have a requirement like binding the xml file with dropdown list

Step 1: Create a new website using Visual Studio 2010 or any other Visual Studio Installed on your system.

Step 2: Create a new XML file with the below XML content. You can rename as depatment.xml file. This is use to bind dropdown list to xml file. Store the above xml file in the application root



  
    1
    Information Technology
  
  
    2
    Human Resource Management
  
  
    3
    Sales Depatment
  
  
    4
    System Network
  
  
    5
    Customer Support
  

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.
 
Step 3: Create a new page with following source codes(aspx web page)

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

 


    Sample binding xml file into dropdown list


    
    
Department :

Selected Department :


Step 4: 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 have learned ,how to bind dropdown list with xml file in asp.net with C# example