Binding data with a data source control
A data source control moves the connection and command details out of your C# and into the page markup, where a grid can bind to them directly.
The pairing
Markup
<asp:SqlDataSource id="productsSource" runat="server"
ConnectionString="<%$ ConnectionStrings:Shop %>"
SelectCommand="SELECT Id, Name, Price FROM Product" />
<asp:GridView id="productsGrid" runat="server"
DataSourceID="productsSource" AutoGenerateColumns="false" />What you gain
- No connection code in the page class.
- Built in support for sorting, paging and parameters.
- The query is visible in the markup, which is convenient for small pages.
Keep it in perspective. A data source control is excellent for straightforward screens. When the logic grows, move it into a class where it can be tested, which is the approach used in web services and reuse.
Add sorting next in letting the visitor sort the grid.