ASP.NET DropDownList with XML Databinding

By Peter A. Bromberg, Ph.D.

Peter Bromberg  

I read the MS newsgroups as often as I can, not only to get (hopefully) answers to my own questions, but also to share ideas and get ideas of my own on how to solve programming problems. Recently I answered somebody's post on microsoft.public.dotnet.xml about how to use databinding from an XML file to populate an ASP.NET DropDownList server control. Not having done this before, I decided to take up the challenge. It proved quite useful, and since I searched the web first and could find no specific examples of this, I was on my own, so I'm going to share the code here.



First, this individual's XML file looked like this:

<?xml version="1.0" encoding="utf-8" ?>
<companytype>
<option value="Agricultural" name="Agricultural" />
<option value="Apparel" name="Apparel" />
<option value="Beverages" name="Beverages" />
<option value="Building Products" name="Building Products" />
</companytype>


Obviously, this looks more like the HTML you'd have in the actual listbox itself, and doesn't lend itself well to databinding. So the first thing I did was rewrite the XML so that it could be loaded into a DataSet and used more efficiently. Following is the revised code for "Xmldroplist.xml":

<?xml version="1.0" encoding="utf-8" ?>
<companytypes>
<companytype>
<value>Agricultural</value>
<name>Agricultural</name>
</companytype>
<companytype>
<value>Apparel</value>
<name>Apparel</name>
</companytype>
<companytype>
<value>Beverages</value>
<name>Beverages</name>
</companytype>
<companytype>
<value>Building Products</value>
<name>Building Products</name>
</companytype>
</companytypes>

Finally, I pieced together the most basic code to create the server control on the page, load the XML into a DataSet, and use dynamic databinding to populate the dropdown listbox. Following is the code for "XMLDropDownList.aspx":

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat="Server">
void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
BindDropDown();
}
}

void BindDropDown() {
string url = "http://localhost/ASP.NET/xmldroplist.xml";
DataSet ds = new DataSet();
ds.ReadXml(url);
customers.DataSource =ds.Tables[0];
customers.DataTextField =ds.Tables[0].Columns[0].ToString();
customers.DataValueField=ds.Tables[0].Columns[1].ToString();
customers.DataBind();

// Next 2 lines show way to set select item text and value...
//customers.Items.Insert(0, new ListItem("TestValue")) ;
//customers.Items[0].Value="84";
// Next line shows another way but using an existing item...
// customers.SelectedIndex=2;

}
</script>
<html>
<body bgcolor="#ffffff">
<form runat="server" ID="Form1">
<b>Select a Company Type to View:</b>
<asp:DropDownList id="customers" runat="server" />
</form>
</body>
</html>


When you run this page you'll get your dropdown listbox with all the values in it, and if you view source you'll see that both the DataTextField and the DataValueField properties have been translated into their proper places in each <option> element.


Peter Bromberg is an independent consultant specializing in distributed .NET solutionsa Senior Programmer /Analyst at in Orlando and a co-developer of the NullSkull.com developer website. He can be reached at info@eggheadcafe.com