数据源控件大大扩展了诸如 GridView、FormView 和 DetailsView 控件等数据绑定控件的功能。通过将数据源控件与数据绑定控件一起使用,您无需编写代码或只需编写少量代码即可从不同的数据源中检索、修改、分页、排序和筛选数据。
使用 DataSourceID 属性进行绑定
您可以将数据绑定控件绑定到诸如 ObjectDataSource 或 SqlDataSource 控件等数据源控件,这样您便可以在数据绑定控件中使用数据。数据源控件连接到数据库或中间层对象等数据源,然后检索或更新数据。之后,数据绑定控件即可使用此数据。要执行绑定,应将数据绑定控件的 DataSourceID 属性设置为指向数据源控件。当数据绑定控件绑定到数据源控件时,您无需编写代码或只需很少的额外代码即可执行数据操作,因为数据绑定控件可自动利用数据源控件所提供的数据服务。
注意 |
---|
在 ASP.NET 的早期版本中,数据绑定控件是使用 DataSource 属性绑定到数据的,并且您需要编写代码才能处理数据的显示、分页、排序、编辑和删除等操作。虽然仍可以使用 DataSource 属性(并使用现有代码)将控件绑定到数据,但现在已改为使用 DataSourceID 属性执行绑定。 |
下面的代码示例演示绑定到 SqlDataSource 控件以显示数据库中的数据的 FormView 控件。
<%@ Page language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <head runat="server"> <title>FormView Example</title> </head> <body> <form id="form1" runat="server"> <h3>FormView Example</h3> <table cellspacing="10"> <tr> <td valign="top"> <asp:FormView ID="ProductsFormView" DataSourceID="ProductsSqlDataSource" AllowPaging="true" runat="server"> <HeaderStyle forecolor="white" backcolor="Blue" /> <ItemTemplate> <table> <tr> <td align="right"><b>Product ID:</b></td> <td><asp:Label id="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' /></td> </tr> <tr> <td align="right"><b>Product Name:</b></td> <td><asp:Label id="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' /></td> </tr> <tr> <td align="right"><b>Category ID:</b></td> <td><asp:Label id="CategoryIDLabel" runat="server" Text='<%# Eval("CategoryID") %>' /></td> </tr> <tr> <td align="right"><b>Quantity Per Unit:</b></td> <td><asp:Label id="QuantityPerUnitLabel" runat="server" Text='<%# Eval("QuantityPerUnit") %>' /></td> </tr> <tr> <td align="right"><b>Unit Price:</b></td> <td><asp:Label id="UnitPriceLabel" runat="server" Text='<%# Eval("UnitPrice") %>' /></td> </tr> </table> </ItemTemplate> <PagerTemplate> <table> <tr> <td><asp:LinkButton ID="FirstButton" CommandName="Page" CommandArgument="First" Text="<<" RunAt="server"/></td> <td><asp:LinkButton ID="PrevButton" CommandName="Page" CommandArgument="Prev" Text="<" RunAt="server"/></td> <td><asp:LinkButton ID="NextButton" CommandName="Page" CommandArgument="Next" Text=">" RunAt="server"/></td> <td><asp:LinkButton ID="LastButton" CommandName="Page" CommandArgument="Last" Text=">>" RunAt="server"/></td> </tr> </table> </PagerTemplate> </asp:FormView> </td> </tr> </table> <asp:SqlDataSource ID="ProductsSqlDataSource" SelectCommand="SELECT * FROM [Products]" connectionstring="<%$ ConnectionStrings:NorthwindConnection %>" RunAt="server"/> </form> </body> </html>
<%@ Page language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <head runat="server"> <title>FormView Example</title> </head> <body> <form id="form1" runat="server"> <h3>FormView Example</h3> <table cellspacing="10"> <tr> <td valign="top"> <asp:FormView ID="ProductsFormView" DataSourceID="ProductsSqlDataSource" AllowPaging="true" runat="server"> <HeaderStyle forecolor="white" backcolor="Blue" /> <ItemTemplate> <table> <tr> <td align="right"><b>Product ID:</b></td> <td><asp:Label id="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' /></td> </tr> <tr> <td align="right"><b>Product Name:</b></td> <td><asp:Label id="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' /></td> </tr> <tr> <td align="right"><b>Category ID:</b></td> <td><asp:Label id="CategoryIDLabel" runat="server" Text='<%# Eval("CategoryID") %>' /></td> </tr> <tr> <td align="right"><b>Quantity Per Unit:</b></td> <td><asp:Label id="QuantityPerUnitLabel" runat="server" Text='<%# Eval("QuantityPerUnit") %>' /></td> </tr> <tr> <td align="right"><b>Unit Price:</b></td> <td><asp:Label id="UnitPriceLabel" runat="server" Text='<%# Eval("UnitPrice") %>' /></td> </tr> </table> </ItemTemplate> <PagerTemplate> <table> <tr> <td><asp:LinkButton ID="FirstButton" CommandName="Page" CommandArgument="First" Text="<<" RunAt="server"/></td> <td><asp:LinkButton ID="PrevButton" CommandName="Page" CommandArgument="Prev" Text="<" RunAt="server"/></td> <td><asp:LinkButton ID="NextButton" CommandName="Page" CommandArgument="Next" Text=">" RunAt="server"/></td> <td><asp:LinkButton ID="LastButton" CommandName="Page" CommandArgument="Last" Text=">>" RunAt="server"/></td> </tr> </table> </PagerTemplate> </asp:FormView> </td> </tr> </table> <asp:SqlDataSource ID="ProductsSqlDataSource" SelectCommand="SELECT ProductID, ProductName, CategoryID, QuantityPerUnit, UnitPrice FROM [Products]" connectionstring="<%$ ConnectionStrings:NorthwindConnection %>" RunAt="server"/> </form> </body> </html>
有关数据源控件的更多信息,请参见数据源 Web 服务器控件。
选择数据
数据源控件检索数据的方式由控件本身决定。ObjectDataSource 控件通过调用 SelectMethod 属性中指定的方法来读取数据。下面的代码示例演示 ObjectDataSource 控件,该控件使用 EmployeeLogic 类的 GetAllEmployees 方法返回数据。
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %> <%@ Page language="vb" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <head> <title>ObjectDataSource - Visual Basic Example</title> </head> <body> <form id="Form1" method="post" runat="server"> <asp:gridview id="GridView1" runat="server" datasourceid="ObjectDataSource1" /> <asp:objectdatasource id="ObjectDataSource1" runat="server" selectmethod="GetAllEmployees" typename="Samples.AspNet.VB.EmployeeLogic" /> </form> </body> </html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %> <%@ Page language="c#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <head> <title>ObjectDataSource - C# Example</title> </head> <body> <form id="Form1" method="post" runat="server"> <asp:gridview id="GridView1" runat="server" datasourceid="ObjectDataSource1" /> <asp:objectdatasource id="ObjectDataSource1" runat="server" selectmethod="GetAllEmployees" typename="Samples.AspNet.CS.EmployeeLogic" /> </form> </body> </html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.JSL" Assembly="Samples.AspNet.JSL" %> <%@ Page language="VJ#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <head> <title>ObjectDataSource - VJ# Example</title> </head> <body> <form id="Form1" method="post" runat="server"> <asp:gridview id="GridView1" runat="server" datasourceid="ObjectDataSource1" /> <asp:objectdatasource id="ObjectDataSource1" runat="server" selectmethod="GetAllEmployees" typename="Samples.AspNet.JSL.EmployeeLogic" /> </form> </body> </html>
有关更多信息,请参见 ObjectDataSource 控件概述。
SqlDataSource 和 AccessDataSource 控件通过运行 SelectCommand 属性中指定的 SQL 查询来选择数据。下面的代码示例演示 SqlDataSource 控件,该控件从 Northwind 示例数据库的 Employees 表中返回数据。
<%@ Page language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <head runat="server"> <title>ASP.NET Example</title> </head> <body> <form id="form1" runat="server"> <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader" ConnectionString="<%$ ConnectionStrings:MyNorthwind%>" SelectCommand="SELECT LastName FROM Employees"> </asp:SqlDataSource> <asp:ListBox id="ListBox1" runat="server" DataTextField="LastName" DataSourceID="SqlDataSource1"> </asp:ListBox> </form> </body> </html>
<%@ Page language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <head runat="server"> <title>ASP.NET Example</title> </head> <body> <form id="form1" runat="server"> <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader" ConnectionString="<%$ ConnectionStrings:MyNorthwind%>" SelectCommand="SELECT LastName FROM Employees"> </asp:SqlDataSource> <asp:ListBox id="ListBox1" runat="server" DataTextField="LastName" DataSourceID="SqlDataSource1"> </asp:ListBox> </form> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <head runat="server"> <title>ASP.NET Example</title> </head> <body> <form id="form1" runat="server"> <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader" ConnectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;" SelectCommand="SELECT LastName FROM Employees"> </asp:SqlDataSource> <asp:ListBox id="ListBox1" runat="server" DataTextField="LastName" DataSourceID="SqlDataSource1"> </asp:ListBox> </form> </body> </html>
有关更多信息,请参见使用 SqlDataSource 控件选择数据。
XmlDataSource 不允许从源 XML 数据中选择特定元素。不过,您可以使用 XPath 属性指定筛选器。有关更多信息,请参见使用 XmlDataSource 控件筛选数据。