Fun Pet Trick 10: Data Binding the New BulletedList Control to an XML File


Fun Pet Trick #10: Data Binding the New BulletedList Control to an XML File

By Scott Guthrie

One of the new server controls in Whidbey is the BulletedList control, which enables you (not surprisingly) to display a list of bulleted items.

One of the nice features of the control is a displaymode property that enables you to toggle the control in a few interesting ways. The default value is text, which causes plain text to be rendered. You can also toggle it into HyperLink mode, which will generate hyperlinks.

A simple trick is to use the BulletedList control (in HyperLink mode) to generate a list of links to other sites within your page, by dynamically binding it to an XML file on disk (where the links are encapsulated).

The following example shows how to easily do this using the new DataSetDataSource control:

 <%@ page language="VB" %> <html> <body>     <form runat="server">         <asp:bulletedlist  runat="server"             datatextfield="Name" datasource             datavaluefield="Url" displaymode="HyperLink">         </asp:bulletedlist>         <br />         <br />         <asp:datasetdatasource              runat="server" datafile="MyLinks.xml">         </asp:datasetdatasource>     </form> </body> </html> 

where the MyLinks.xml file is defined as follows:

 <?xml version="1.0" encoding="utf-8" ?> <Links>     <Link>         <Name>ASP.NET Site</Name>         <Url>http://www.asp.net</Url>     </Link>     <Link>         <Name>MSDN</Name>         <Url>http://msdn.microsoft.com</Url>     </Link>     <Link>         <Name>ASPAdvice</Name>         <Url>http://www.aspadvice.com</Url>     </Link> </Links> 

Updating the XML file on disk will then automatically update the list of links on your site.

A very simple trick—but kind of useful on content sites.




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net