写了个小程序,第一页正确的,分页显示后,第二页提示CategoryID为空了程序就报错,怎样解决?,初学者求救!
原代码如下:<%@ Page Language="C#" %> 
<script runat="server"> 
void categoryView_RowDataBound(object sender, GridViewRowEventArgs e) 

if (e.Row.RowType == DataControlRowType.DataRow) 

// 在第二列获取GridView控件 
GridView productView = (GridView)e.Row.Cells[1].Controls[1]; 
// 为当前类别设置CategoryID参数,用于返回产品列表 
string categoryID = 
categoryView.DataKeys[e.Row.DataItemIndex].Value.ToString(); 
productSource.SelectParameters[0].DefaultValue = categoryID ; 
productView.DataSource = 
productSource.Select(DataSourceSelectArguments.Empty); 
productView.DataBind(); 


</script> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<title>Embedding Child GridView within a Parent GridView </title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
<asp:GridView ID="categoryView" DataKeyNames="ProductSubcategoryID" runat="server" AutoGenerateColumns="false" DataSourceID="categorySource" AllowPaging="True" PageSize="2" OnRowDataBound="categoryView_RowDataBound" HeaderStyle-BackColor="blue" HeaderStyle-ForeColor="White"> 
<Columns> 
  <asp:TemplateField HeaderText="Category"> 
      <ItemStyle VerticalAlign="Top" Width="20%" /> 
        <ItemTemplate> 
        <br/> <b> <%# Eval("ProductSubcategoryID") %> </b> 
        <br/> <br/> <%# Eval("Name") %> <br /> 
      </ItemTemplate> 
  </asp:TemplateField> 
    <asp:TemplateField HeaderText="Products"> 
  <ItemStyle VerticalAlign="Top" /> 
  <ItemTemplate> 
      <asp:GridView AutoGenerateColumns="false" ID="productView" Runat="server"> 
    <Columns> 
        <asp:BoundField DataField="Name" HeaderText="Product Name" /> 
        <asp:BoundField DataField="ProductNumber" HeaderText="Product Number" /> 
        <asp:BoundField DataField="ListPrice" HeaderText="List Price" DataFormatString="{0:C}" /> 
      </Columns> 
      </asp:GridView> 
  </ItemTemplate> 
  </asp:TemplateField> 
  </Columns> 
</asp:GridView> 
<asp:SqlDataSource ID="categorySource" Runat="server" SelectCommandType="Text" SelectCommand="Select ProductSubcategoryID,Name from ProductSubcategory" ConnectionString=" <%$ ConnectionStrings:NorthwindConnectionString %>"> 
</asp:SqlDataSource> 
<asp:SqlDataSource ID="productSource" Runat="server" SelectCommandType="Text" SelectCommand="Select Name,ProductNumber, ListPrice from Product Where ProductSubcategoryID = @ProductSubcategoryID" ConnectionString=" <%$ ConnectionStrings:NorthwindConnectionString %>"> 
<SelectParameters> 
<asp:Parameter Name="ProductSubcategoryID" Type="String" /> 
</SelectParameters> 
</asp:SqlDataSource> 
</div> 
</form> 
</body> 
</html>

解决方案 »

  1.   

    asp.net夜话之八:数据绑定控件 
    在asp.net中所有的数据库绑定控件都是从BaseDataBoundControl这个抽象类派生的,这个抽象类定义了几个重要属性和一个重要方法:DataSource属性:指定数据绑定控件的数据来源,显示的时候程序将会从这个数据源中获取数据并显示。DataSourceID属性:指定数据绑定控件的数据源控件的ID, 显示的时候程序将会根据这个ID找到相应的数据源控件,并利用这个数据源控件中指定方法获取数据并显示。DataBind()方法:当指定了数据绑定控件的DataSource属性或者DataSourceID属性之后,再调用DataBind()方法才会显示绑定的数据。并且在使用数据源时,会首先尝试使用DataSourceID属性标识的数据源,如果没有设置DataSourceID时才会用到DataSource属性标识的数据源。也就是说DataSource和DataSourceID两个属性不能同时使用。数据绑定控件的DataSource控件属性必须是一个可以枚举的数据源,如实现了ICollection、IEnumerable或IListSource接口的类的实例。本节主