页面功能:
1、通过列表的形式显示所有书籍。
2、选择书籍类别,通过列表的形式显示指定类别的书籍。
3 、上面两个功能均需实现高效分页。
使用控件:
1、ListView
2、ObjectDataSource
3 、DropDownList
略过部分:DAL层的高效分页方法、UI层ListView配置、DropDownList的数据源配置和绑定
特别记忆部分:
上述功能在 BLL层所需的方法如下图所示:
ObjectDataSource 配置发如下:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetAll" TypeName="BookShop.BLL.BooksBLL" EnablePaging="True"
SelectCountMethod="CountBooks" onload="ObjectDataSource1_Load" >
</asp:ObjectDataSource>
在这里可以看到BLL层中GetAll和CountBooks是具有重载的方法。程序在运行过程中,如何让ObjectDataSource 选择我们所需要的方法呢?
通过查阅MSDN,在ObjectDataSource.SelectMethod 属性中有如下描述:
所以,我决定在ObjectDataSource的Selecting事件中通过添加“catId”的参数的办法,使ObjectDataSource动态选择BLL层中的哪一个重载方法。
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (ddr_Ca.SelectedIndex != 0) // 判断是否指定了书籍类别,ddr_Ca.SelectedIndex ==0则没有指定
// ddr_Ca.SelectedIndex !=0则指定了书籍类别
{
ObjectDataSource1.SelectParameters.Clear();
ObjectDataSource1.SelectParameters.Add("catId", ddr_Ca.SelectedValue);
}
页面运行后发现,页面呈现的数据有“滞后”现象。备注(DropDownList的AutoPostBack="True"),选择相应的书籍类别后,并没有呈现相应类别的图书。点击下一页后,才会出现相应类别的图书。
所以我决定将下面代码放到ObjectDataSource的Load事件中,代码如下:


1 protected void ObjectDataSource1_Load(object sender, EventArgs e) 2 { 3 if (ddr_Ca.SelectedIndex != 0) 4 { 5 ObjectDataSource1.SelectParameters.Clear(); 6 ObjectDataSource1.SelectParameters.Add("catId", ddr_Ca.SelectedValue); 7 } 8 }
上述功能点完成。
还需要特别注意的地方:
一、若是通过使用向导配置ObjectDataSource,SelectParameters中会自动生成两个参数:startRowIndex和maximumRows。
<SelectParameters>
<asp:Parameter Name="startRowIndex" Type="Int32" />
<asp:Parameter Name="maximumRows" Type="Int32" />
</SelectParameters>
启动分页后页面运行,会出现下面异常。解决办法是删除SelectParameters中的这两个参数。
异常详细信息: System.InvalidOperationException: ObjectDataSource“ObjectDataSource1”未能找到带参数的非泛型方法“CountBooks”: startRowIndex, maximumRows。
二、SelectMethod中的分页参数的名称必须是startRowIndex和maximumRows
三、SelectMethod和SelectCountMethod重载中的参数名称要一样,因功能已实现,没有测试参数名称不一样会有什么后果。