Data source controls greatly expand the capabilities of data-bound controls such as the
Binding Using the DataSourceID Property
You can work with data in a data-bound control by binding the data-bound control to a data source control such as an
![]() |
---|
In previous versions of ASP.NET, data-bound controls were bound to data using the |
The following code example shows a FormView control bound to a SqlDataSource control to display data from a database.
Visual BasicВ | ![]() |
---|---|
<%@ Page language="VB" %> <html> <body> <form 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> |
C#В | ![]() |
---|---|
<%@ Page language="C#" %> <html> <body> <form 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> |
For more information on data source controls, see Data Source Web Server Controls.
Selecting Data
The way in which a data source control retrieves data is determined by the control itself. The ObjectDataSource control reads data by calling the method specified in the GetAllEmployees
method of the EmployeeLogic
class:
Visual BasicВ | ![]() |
---|---|
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %> <%@ Page language="vb" %> <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> |
C#В | ![]() |
---|---|
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %> <%@ Page language="c#" %> <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> |
J#В | ![]() |
---|---|
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.JSL" Assembly="Samples.AspNet.JSL" %> <%@ Page language="VJ#" %> <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> |
For more information, see ObjectDataSource Control Overview.
The SqlDataSource and
Visual BasicВ | ![]() |
---|---|
<%@ Page language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <FORM 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> |
C#В | ![]() |
---|---|
<%@ Page language="CS" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <FORM 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> |
J#В | ![]() |
---|---|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <FORM 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> |
For more information, see Selecting Data Using the SqlDataSource Control.
The
Modifying Data
The ObjectDataSource and SqlDataSource controls support modifying bound data. For more information, see Modifying Data using Data Source Controls.