写了个小程序,第一页正确的,分页显示后,第二页提示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>
原代码如下:<%@ 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>
解决方案 »
- GridView点击“编辑”按钮后如何操作TextBox里的字符串?
- 我想使用泛型,而又没有对应的Model,应该怎么办?
- net Calendar控件怎么在旁边加一个标志
- 列位!请问一下。如何按以下的方式选择一个数据库的栏目列表?请看内文!
- 如何获取一个已绑定好数据源的GridView的列数据?
- 求一sql语句
- Session_OnEnd函数没有反映
- 招聘.Net开发人员 北京
- 100 分 !!寻.net 中的水晶报表的注册码!!!!
- 在Microsoft Visual Studio .NET 中,将控件进行排版真是个问题!
- 这段代码在那里设置打水印坐标呢?
- javascript------------小白代码问题
在asp.net中所有的数据库绑定控件都是从BaseDataBoundControl这个抽象类派生的,这个抽象类定义了几个重要属性和一个重要方法:DataSource属性:指定数据绑定控件的数据来源,显示的时候程序将会从这个数据源中获取数据并显示。DataSourceID属性:指定数据绑定控件的数据源控件的ID, 显示的时候程序将会根据这个ID找到相应的数据源控件,并利用这个数据源控件中指定方法获取数据并显示。DataBind()方法:当指定了数据绑定控件的DataSource属性或者DataSourceID属性之后,再调用DataBind()方法才会显示绑定的数据。并且在使用数据源时,会首先尝试使用DataSourceID属性标识的数据源,如果没有设置DataSourceID时才会用到DataSource属性标识的数据源。也就是说DataSource和DataSourceID两个属性不能同时使用。数据绑定控件的DataSource控件属性必须是一个可以枚举的数据源,如实现了ICollection、IEnumerable或IListSource接口的类的实例。本节主