Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Learn to Use the New Data Source Controls in ASP.NET 2.0 : Page 3

You can declaratively connect your data sources to Web form controls with ease in ASP.NET 2.0. In this article, three new data source controls are described in detail: ObjectDataSource, DataSetDataSource, and XmlDataSource.


advertisement
The DataSetDataSource Control
The DataSetDataSource control allows you to manipulate an XML document just like a dataset. Suppose you have the following XML document (Books.xml) in your project:

<?xml version="1.0" standalone="yes"?> <Books xmlns="http://tempuri.org/Books.xsd"> <Book> <Title>ASP.NET 2.0: A Developer's Notebook (O'Reilly) </Title> <PubDate>December 2004</PubDate> <Synopsis>To bring you up to speed with ASP.NET 2.0, this practical book offers nearly 50 hands-on projects.
Each one explores a new feature of the language, with emphasis on changes that can increase productivity, simplify
programming tasks, and help you add new functionality to your applications. You get the goods straight from the
masters in an informal, code-intensive style. Part of our new Developer's Notebook series.</Synopsis> </Book> <Book> <Title>.NET Compact Framework Pocket Guide (O'Reilly) </Title> <PubDate>May 2004</PubDate> <Synopsis>Looking to create applications for Pocket PC and Windows based Smartphones? The new .NET Compact
Framework Pocket Guide will teach you what you need to know. This handy reference provides a quick tour of the
.NET Compact Framework and includes several working projects to get you productive straightaway. Whether you're
new to mobile programming or new to Visual Studio .NET 2003, this pocket guide will get you started writing mobile
applications. </Synopsis> </Book> <Book> <Title>Windows XP Unwired (O'Reilly)</Title> <PubDate>August 2003</PubDate> <Synopsis>Windows XP Unwired provides a complete introduction to all the wireless technologies supported by
Windows XP, including Wi-Fi (802.11b, a, and g), infrared, Bluetooth, CDMA2000, and GPRS. It's a one-stop wireless
information source for technically savvy Windows XP users. This book will show you the full-spectrum view of
wireless capabilities of Windows XP, and how to take advantage of them. </Synopsis> </Book> </Books>

Figure 11. All Set: Here's the running .aspx page, which displays records from books.xml using the GridView control.
I will use the DataSetDataSource control to bind this XML document to the GridView control. Drag-and-drop a DataSetDataSource control onto the default page. Click on the 'Configure Data Source…' link in the Smart Tag (see Figure 10).

In the Configure Data Source window, enter "~/Books.xml" for the data file, and click OK. Drag-and-drop another GridView control to the page and bind it to DataSetDataSource1. Check the Enable Paging, Enable Sorting, and Enable Selection options.



Figure 14. Make a New Key: Specify a key in the schema file.
That's it! Press F5 to load the page. The GridView control will display the information in the XML file (see Figure 11).

Using the GridView control, you can also enable the editing of the XML document through the following steps.

First, set the ReadOnly property of the DataSetDataSource control to False. This will allow the XML document to be modified. Next, enable Editing on the GridView control (see Figure 12). Set the DataKeyNames property of the GridView control to Title.

You also need to generate a schema for the XML document. You can do this in Visual Studio 2005 by double-clicking the XML document in Solution Explorer and then right-clicking on it and selecting Create Schema (see Figure 13).

Figure 15. Change Title: You can now edit the GridView control.
In the schema file that is created, right-click on the Title element (see Figure 14) and then select Add—>New key…. This will define the Title element as the key identifying the individual rows in the GridView control. Then check the dataset primary key checkbox and click OK.

Finally, configure the DataSetDataSource control by specifying the name of the schema file "~/.Books.xsd," in the field under the Data file name. With that, the GridView control is editable (see Figure 15).



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap