Saturday, April 25, 2009

Bind asp.net dropdownlist to xml file


Fig: Binding asp.net dropdownlist control to an xml file

Today I am discussing the simple way to bind an asp.net dropdownlist control to an xml file. Besides binding the dropdownlist controls to an xml file, we will also sort the items in the ascending order.

Let us define the xml file first. This consists of Country object with properties ID and Name.

<?xml version="1.0" encoding="utf-8" ?>
<Countries>
<Country>
<ID>1</ID>
<Name>Nepal</Name>
</Country>
<Country>
<ID>2</ID>
<Name>India</Name>
<Country>
<ID>3</ID>
<Name>China</Name>
</Country>
<Country>
<ID>4</ID>
<Name>Bhutan</Name>
</Country>
<Country>
<ID>5</ID>
<Name>USA</Name>
</Country>
</Country>
</Countries>

We place this xml file in the Resources folder of the root of our solution. Now let us define the markup for the asp.net dropdownlist control.

Select from the list of countries:</b>&nbsp;<asp:DropDownList ID="ddlCountry" runat="server"
Width="160px"></asp:DropDownList>

Now in the load event of the page, we read the items from the xml file into a dataset. We will get the DataView for the default table in the dataset, and sort it. The table will consists of as many rows as there are country objects in the xml file. Each row has two columns: ID and Name. Now we can bind this dataview to the dropdownlist control, as in the code (in C#) below.

//page load event handler
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//call to the function to populate dropdownlist from xml
PopulateDDLFromXMLFile();
}
}

//populates the dropdownlist from xml file
public void PopulateDDLFromXMLFile()
{
DataSet ds = new DataSet();
ds.ReadXml(MapPath("~/Resources/XMLFile.xml"));

//get the dataview of table "Country", which is default table name
DataView dv = ds.Tables["Country"].DefaultView;
//or we can use:
//DataView dv = ds.Tables[0].DefaultView;

//Now sort the DataView vy column name "Name"
dv.Sort = "Name";

//now define datatext field and datavalue field of dropdownlist
ddlCountry.DataTextField = "Name";
ddlCountry.DataValueField = "ID";

//now bind the dropdownlist to the dataview
ddlCountry.DataSource = dv;
ddlCountry.DataBind();
}

That simple! Happy Programming!

kick it on DotNetKicks.com Shout it pimp it

8 comments:

Anonymous said...

hi,
it doesn't work. When I run it, I got an error message :
The DataSourceID of 'ddlProject' must be the ID of a control of type IDataSource. A control with ID 'XmlDataSourceXML' could not be found.
any suggestion please ?

Chirag said...

Nice one it work for me

Anonymous said...

There is no way to bind text and value from xml to DropDownLists. Typical Microsoft.

Anonymous said...

Helpful, Thanks

Anonymous said...

it gives this error to me.. :(
DataBinding: 'System.Char' does not contain a property with the name 'name'

Anonymous said...

Thanks....... Good post.... everything is working.......

Anonymous said...

For those it's not working, you need to add "using System.Data;" in the header..Author should have mentioned it for the beginners..Good article otherwise..

Bharat said...

Thanks for sharing this Info..

Post a Comment

Hope you liked this post. You can leave your message or you can put your valuable suggestions on this post here. Thanks for the sharing and cooperation!

Popular Posts

Recent Articles