ASP. NET dropdownlist with XML databinding

Source: Internet
Author: User
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>
Obviusly, 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 cocould 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.