ASP.NET 2.0: DataGrid Sample

ASP.NET 2.0 streamlines data-driven application development. This example uses the categories and product tables in SQL Server's Northwind sample database, displaying the categories in a dropdown list. This code listing uses the selected category in the dropdown list to display all the products in that category in a GridView control.

<%@ page language="C#" %>
<html>
<
headrunat="server"ID="Head1"NAME="Head1">
            <title>SqlDataSource
            Example
            </title>
</
head>
<
body>
<
formrunat="server"ID="Form1">               
      <asp:sqldatasource
           
id="categoriesDataSource"    
           
runat="server"
           
datasourcemode="DataSet"
           
connectionstring="Data
            Source=localhost;uid=sa;
            pwd=;Initial
            Catalog=Northwind;"
            selectcommand="usp_GetCategories">
      </asp:sqldatasource>
      Select Categories:
      <asp:dropdownlist
           
id="ddlCategoriesList"
           
runat="server"
           
datavaluefield="CategoryID"
           
datasourceid="categoriesDataSource"
           
autopostback="true"
            datatextfield="CategoryName">
      </asp:dropdownlist>
      <asp:sqldatasourcerunat="server"
           
id="productsDataSource"
           
datasourcemode="DataReader"
            connectionstring="Data
            Source=localhost;uid=sa;pwd=;
            Initial Catalog=Northwind;"
            selectcommand="SELECT ProductID,
            ProductName, QuantityPerUnit,
            UnitPrice  FROM Products Where
            CategoryID=@CategoryID">
            <selectparameters>
            <asp:controlparameter
                 
defaultvalue="1"
                 
name="CategoryID"
                 
propertyname="SelectedValue"
                 
controlid="ddlCategoriesList">
            </asp:controlparameter>
            </selectparameters>
      </asp:sqldatasource>
      <br/><b><br/>
      Product Details<br/></b><br/>
      <asp:gridview
           
id="productsGrid"runat="server"
           
datasourceid="productsDataSource">
      </asp:gridview>
</
form>
</
body>
</
html>

Written on February 11, 2004