DataSource series controls in ASP.net 2.0

Source: Internet
Author: User
Tags bind integer xpath access database visual studio
Asp.net| Control asp.net 2.0, there is a great deal of improvement in the data connection, the newly added DataSource series controls make it easier to connect to the database, and many can complete the writing of SQL statements and database connections through a wizard-style setup. The DataSource series controls in ASP.net 2.0 total 6 species, respectively:

SqlDataSource Control----Data source control used to connect to a SQL database
AccessDataSource Control----Data source control used to connect to an Access database
The ObjectDataSource control----The data source control used to connect the custom object
Datasetdatasource Control-----A control that makes an XML file a dataset and does related processing
The XmlDataSource Control-----The control to load the XML file and bind to controls such as the DataGrid, DataList, and so on
The SiteMapDataSource control-----The control to load a predefined site layout file and then bind it to the TreeNode tree control or SiteMapPath control to make the site's page navigation easier.

 
In this article, you will focus on ObjectDataSource controls, Datasetdatasource controls, and XmlDataSource controls, and SqlDataSource controls, refer to the use of ASP.net The GridView control in 2.0, which describes how the SqlDataSource control is used, and the AccessDataSource control is similar to the SqlDataSource control, except that the connected database is access.

ObjectDataSource Control

The control that binds the objects created by the user to the data control, such as binding to Datagrid,gridview. For example, in Visual Studio 1, create a new site and add a new class named Products:

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class Products
Public Function getproducts () as DataSet
Dim Conn as New SqlConnection ("server= (local); integrated security=true;database=northwind; Persist
Security Info=true ")
Dim Adapter as New SqlDataAdapter ("SELECT [ProductID], [ProductName], [SupplierID], [CategoryID],
[QuantityPerUnit], [UnitPrice] from [Products], conn)

Dim DS as New DataSet
Adapter. Fill (ds, "products")
Return DS
End Function
End Class
The Product class contains the GetProducts method, which returns all the products in the Northwind database and returns as a dataset. With the ObjectDataSource control, you can bind a custom class to a data control, but simply drag the Ojectdatasource control into the design form, and then click the ' Configure Data Source ... ' link, In the pop-up form (pictured below), select the class you want to bind, and then select the Product class.


In the next step, select the related method in which class you want to bind

In the next step, you will be able to choose which SQL statements to perform, such as Select,update,insert,delete, and only return product data in this article, so choose Select and then finish the operation.

Next, drag a GridView control into the form, bind it to the ObjectDataSource control we just created, and tick the Enable paging, enable scripting, enable selection three selection boxes, The following figure:


After running the program, you can see the results. If you want to edit the Ojectdatasource control, you need to provide a different method, we add a method called Updateproducts, as follows:

Public Sub updateproducts (ByVal ProductID as Integer, ByVal ProductName as String, _
ByVal SupplierID As Integer, ByVal CategoryID As Integer, _
ByVal QuantityPerUnit as String, ByVal UnitPrice as Double)
Dim Conn as New SqlConnection ("server= (local); integrated security=true;database=northwind; Persist Security
Info=true ")
Dim Adapter as New SqlDataAdapter ("SELECT * FROM Products WHERE productid=" & ProductID, Conn)
Dim DS as New DataSet
Adapter. Fill (ds, "products")
With DS. Tables (0). Rows (0)
. Item ("ProductName") = ProductName
. Item ("SupplierID") = SupplierID
. Item ("categoryid") = CategoryID
. Item ("quantityperunit") = QuantityPerUnit
. Item ("UnitPrice") = UnitPrice
End With
Dim CB as New SqlCommandBuilder (adapter)
Adapter. Update (ds, "products")
End Sub
Then bind to the ObjectDataSource control and select the Updateproducts method in the Update tab, and when binding to the GridView control, choose Enable Editing option to run the program, You can edit the record

Datasetdatasource Control

The control allows the XML document or other files to be treated as a dataset, such as having an XML file named after the Books.xml file:

<?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 </PubDate>
<Synopsis> to bring the speed with ASP.net and 2.0, this practical book offers nearly.
. </Synopsis>
</Book>
<Book>
<Title>. NET Compact Framework Pocket Guide (O ' Reilly)
</Title>
<PubDate> May, </PubDate>
<Synopsis> looking to create applications for Pocket PC and Windows based smartphones? </Synopsis>
</Book>
</Books>
Next, the XML file is bound to the GridView using the Datasetdatasource control. Drag the Datasetdatasource control to the design form and select "Configure data Source," in the Data Sources setup form, Select Books.xml as the data source, and then drag a GridView control to bind it to the Datasetdatasource control

XmlDataSource Control
 
The control also allows the XML document or other files to be bound to datagrid,gridview, but the structure of the bound XML file can be very irregular and does not contain a dataset. The XmlDataSource control can also use XPath to bind an XML file to other controls, such as the TreeView. For example, an RSS file, whose XML is represented below, is saved as Msdn.xml:

  

Drag a XmlDataSource control, point ' Configure data Source ... ' link, set its datasource to Msdn.xml, in XPath expression, set to ' Rss/channel/item ', return only the item node, Drag a DataList control and set its data source to XmlDataSource.

In the Smart tag menu, select ' Auto Format ... ' and select Slate scheme and switch to the Code window to add the following code:

<asp:datalist id= "DataList1" runat= "Server"
gridlines= "Horizontal"
Borderwidth= "1px" backcolor= "White" cellpadding= "3"
Borderstyle= "None" bordercolor= "#E7E7FF"
Datasourceid= "XmlDataSource1" >
<footerstyle forecolor= "#4A3C8C"
Backcolor= "#B5C7DE" > </FooterStyle>
<ItemTemplate>
<b> <% #XPath ("title")%> </b> <BR/>
<i> <% #XPath ("pubdate")%> </i> <BR/>
<% #XPath ("description")%><BR/>
<a Href= ' <% #XPath ("link")%> ' >link </a> <BR/>
<BR/>
</ItemTemplate>
<alternatingitemstyle backcolor= "#F7F7F7" >
</AlternatingItemStyle>
<itemstyle forecolor= "#4A3C8C"
Backcolor= "#E7E7FF" >
</ItemStyle>
<selecteditemstyle forecolor= "#F7F7F7"
Font-bold= "True"
Backcolor= "#738A9C" > </SelectedItemStyle>
<HeaderTemplate> RSS Feeds </HeaderTemplate>
Font-bold= "True"
Backcolor= "#4A3C8C" > </HeaderStyle>
</asp:DataList>
Run, you can see a simple RSS form of the reader, as shown below, very convenient.


For SiteMapDataSource controls, refer to the article "using the page navigation control in asp.net 2.0"

Summarize:

This article briefly introduces the very powerful new DataSource series controls in ASP.net 2.0, which are handy in operations related to databases and other data sources, and are expected to add more functionality in the official version of ASP.net 2.0.

Related Article

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.