Tuesday 11 February 2014

Bind Dropdownlist to xml File aspnet

Bind Populate Asp.Net C# Dropdown List from XML File using Dataset with Example

In this Asp.Net Tutorials i will explain how to bind or populate  xml file data to dropdownlist using dataset in asp.net c#.Let’s create an example to implement dropdownlist binding with xml file.

First let’s create a website. So start visual studio – File Menu—New – Website –Name the website and click ok.

Initially we create an xml file. We have named the xml file as states.xml. This xml file contains some states name from india. This  states will be populated to dropdownlist using asp.net c#.To create xml file open website menu ,select add new item, select xml file name it as states.xml,save it in root folder.After creating the xml file add below markup to states.xml


In the .aspx page add the dropdownlist control. In the designer page we have named the dropdownlist control as ddlstates

In the .aspx page add the dropdownlist control. In the designer page we have named the dropdownlist control as ddlstates The name of the state will be assigned to text part and id will be assigned to value part of asp.net drop down list control

    
  
   
This below namespaces needs to be added in code behind in order to bind xml to dropdownlist
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

To bind the dropdownlist to xml file we have created a method in page load method and named it as BindXmlToDropdown().
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
     {
        BindXmlToDropDown();
     }
            
}
private void BindXmlToDropDown()
{
            string filePath = Server.MapPath("~/States.xml");
            using (DataSet ds = new DataSet())
            {
                ds.ReadXml(filePath);
                ddlStates.DataSource = ds;
                ddlStates.DataTextField = "name";
                ddlStates.DataValueField = "id";
                ddlStates.DataBind();
            }
}

Complete C# code to populate dropdownlist from xml file 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace XMLExamples
{
    public partial class DropdownListExample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindXmlToDropDown();
            }
            
        }

        private void BindXmlToDropDown()
        {
            string filePath = Server.MapPath("~/States.xml");
            using (DataSet ds = new DataSet())
            {
                ds.ReadXml(filePath);
                ddlStates.DataSource = ds;
                ddlStates.DataTextField = "name";
                ddlStates.DataValueField = "id";
                ddlStates.DataBind();
            }
        }
    }
}


In this asp.net tutorial we have learned how to bind asp.net dropdownlist to xml file using dataset



No comments:

Post a Comment