`
king_tt
  • 浏览: 2274751 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

GridView高效分页+搜索的完整实现

 
阅读更多

前言:

公司项目开发,上周的任务是做基础数据的管理。在Sharepoint2010里边内嵌asp.net的aspx页,遇到了各种各样奇葩的问题,因为之前对sharepoint只是有一些了解,但是没有设计到具体的编程工作,这一次算是初次接触吧。其中有一部分基础数据数据量很大,大致有十多万,因为是对基础数据的维护,所以还需要对数据进行列表展示,增删改查什么的,大家都知道Asp.net里边的GridView有自带的分页,但是,那个分页对于少量的数据还好,对于这种数十万的数据量而言,这种分页方式简直就是灾难。网上关于GridView高效分页的东西有很多,找了一个自己改了改。用着效果还不错,和大家分享一下。

这是分页的效果图

下边就讲一下具体的实现,首先声明,这个东西是不是我原创的,只是在此基础上进行了修饰和完善。希望能给各位有所启发。

一、前台布局

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<div>
<divid="main">
<divid="search">
<table>
<tr>
<td>
<asp:LabelID="lb"runat="server"Text="姓名"></asp:Label></td>
<td>
<asp:TextBoxID="SearchName"runat="server"></asp:TextBox>
</td>
<td>
<asp:ButtonID="btnSearch"runat="server"Text="查询"onclick="PagerBtnCommand_OnClick"CommandName="search"/>
</td>
<td>
<asp:ButtonID="btnReset"runat="server"Text="重置"onclick="btnReset_Click"/>
</td>
</tr>
</table>
</div>
<divid="gridView">
<asp:GridViewID="UserGridView"runat="server"AutoGenerateColumns="false">
<Columns>
<asp:TemplateFieldHeaderText="用户名">
<ItemTemplate>
<asp:LabelID="UserName"runat="server"Text='<%#Eval("username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldHeaderText="单位名称">
<ItemTemplate>
<asp:LabelID="DisplayName"runat="server"Text='<%#Eval("displayname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldHeaderText="组织编码">
<ItemTemplate>
<asp:LabelID="OrgCode"runat="server"Text='<%#Eval("orgcode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldHeaderText="组织名称">
<ItemTemplate>
<asp:LabelID="OrgName"runat="server"Text='<%#Eval("orgname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<divid="page">
<table>
<tr>
<td>
<asp:LabelID="lbcurrentpage1"runat="server"Text="当前页:"></asp:Label>
<asp:LabelID="lbCurrentPage"runat="server"Text=""></asp:Label>
<asp:LabelID="lbFenGe"runat="server"Text="/"></asp:Label>
<asp:LabelID="lbPageCount"runat="server"Text=""></asp:Label>
</td>
<td>
<asp:LabelID="recordscount"runat="server"Text="总条数:"></asp:Label>
<asp:LabelID="lbRecordCount"runat="server"Text=""></asp:Label>
</td>
<td>
<asp:ButtonID="Fistpage"runat="server"CommandName=""Text="首页"OnClick="PagerBtnCommand_OnClick"/>
<asp:ButtonID="Prevpage"runat="server"CommandName="prev"Text="上一页"
OnClick="PagerBtnCommand_OnClick"/>
<asp:ButtonID="Nextpage"runat="server"CommandName="next"Text="下一页"OnClick="PagerBtnCommand_OnClick"/>
<asp:ButtonID="Lastpage"runat="server"CommandName="last"Text="尾页"
key="last"OnClick="PagerBtnCommand_OnClick"/>
</td>
<td>
<asp:LabelID="lbjumppage"runat="server"Text="跳转到第"></asp:Label>
<asp:TextBoxID="GotoPage"runat="server"Width="25px"></asp:TextBox>
<asp:LabelID="lbye"runat="server"Text="页"></asp:Label>
<asp:ButtonID="Jump"runat="server"Text="跳转"CommandName="jump"OnClick="PagerBtnCommand_OnClick"/>
</td>
</tr>
</table>
</div>
</div>
</div>

  布局的效果如下:

二、后台的代码实现

我会一点一点向大家讲解清楚,这个分页的原理

Members:这里主要定义几个全局的变量,主要用来记录信息的数量、页的数量和当前页

?
#region Members
constintPAGESIZE = 10;//每页显示信息数量
intPagesCount, RecordsCount;//记录总页数和信息总条数
intCurrentPage, Pages, JumpPage;//当前页,信息总页数(用来控制按钮失效),跳转页码
conststringCOUNT_SQL = "select count(*) from p_user";
#endregion

Methods:  

1、GetRecordsCount:该方法主要用来获取当前信息的总数,有一个sqlSearch参数,默认的为default,即初始化页面时,查询所有信息的总条数,当用户输入要搜索的用户名进行检索时,获取符合用户检索条件的信息的总条数

?
/// <summary>
/// 获取信息总数
/// </summary>
/// <param name="sqlSearch"></param>
/// <returns></returns>
publicstaticintGetRecordsCount(stringsqlRecordsCount)
{
stringsqlQuery;
if(sqlRecordsCount == "default")
{
sqlQuery = COUNT_SQL;
}
else
{
sqlQuery = sqlRecordsCount;
}
intRecordCount = 0;
SqlCommand cmd = newSqlCommand(sqlQuery, Conn());
RecordCount = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Connection.Close();
returnRecordCount;
}

2、OverPage:该方法主要用来计算剩余页,当前设置的为每页显示10条数据,如何符合条件的数据有11条,则要显示2页

?
/// <summary>
/// 计算余页
/// </summary>
/// <returns></returns>
publicintOverPage()
{
intpages = 0;
if(RecordsCount % PAGESIZE != 0)
pages = 1;
else
pages = 0;
returnpages;
}

3、ModPage:该方法也是用计算余页,主要用来防止SQL执行溢出

?
/// <summary>
/// 计算余页,防止SQL语句执行时溢出查询范围
/// </summary>
/// <returns></returns>
publicintModPage()
{
intpages = 0;
if(RecordsCount % PAGESIZE == 0 && RecordsCount != 0)
pages = 1;
else
pages = 0;
returnpages;
}

4、Conn:该方法用来创建数据连接对象,在使用的时候只需改成自己的数据库名即可 

?
/// <summary>
/// 数据连接对象
/// </summary>
/// <returns></returns>
publicstaticSqlConnection Conn()
{
SqlConnection conn = newSqlConnection("data source=.;initial catalog=DB_GSL_ZCW;Integrated Security=true");
conn.Open();
returnconn;
}

5、GridViewDataBind:该方法主要用来数据绑定,如果传入的参数为default则,默认的绑定所有的数据,否则,则绑定过滤过的数据

?
/// <summary>
/// GridView数据绑定,根据传入参数的不同,进行不同方式的查询,
/// </summary>
/// <param name="sqlSearch"></param>
privatevoidGridViewDataBind(stringsqlSearch)
{
CurrentPage = (int)ViewState["PageIndex"];
//从ViewState中读取页码值保存到CurrentPage变量中进行按钮失效运算
Pages = (int)ViewState["PagesCount"];
//从ViewState中读取总页参数进行按钮失效运算
//判断四个按钮(首页、上一页、下一页、尾页)状态
if(CurrentPage + 1 > 1)//当前页是否为首页
{
Fistpage.Enabled = true;
Prevpage.Enabled = true;
}
else
{
Fistpage.Enabled = false;
Prevpage.Enabled = false;
}
if(CurrentPage == Pages)//当前页是否为尾页
{
Nextpage.Enabled = false;
Lastpage.Enabled = false;
}
else
{
Nextpage.Enabled = true;
Lastpage.Enabled = true;
}
DataSet ds = newDataSet();
stringsqlResult;
//根据传入参数sqlSearch进行判断,如果为default则为默认的分页查询,否则为添加了过滤条件的分页查询
if(sqlSearch == "default")
{
sqlResult = "Select Top "+ PAGESIZE + "user_serialid,username,displayname,orgcode,orgname from p_user where user_serialid not in(select top "+ PAGESIZE * CurrentPage + " user_serialid from p_user order by user_serialid asc) order by user_serialid asc";
}
else
{
sqlResult = sqlSearch;
}
SqlDataAdapter sqlAdapter = newSqlDataAdapter(sqlResult, Conn());
sqlAdapter.Fill(ds, "Result");
UserGridView.DataSource = ds.Tables["Result"].DefaultView;
UserGridView.DataBind();
//显示Label控件lbCurrentPaget和文本框控件GotoPage状态
lbCurrentPage.Text = (CurrentPage + 1).ToString();
GotoPage.Text = (CurrentPage + 1).ToString();
sqlAdapter.Dispose();
}

6、Page_Load:页面加载函数,主要是在首次进入页面时,进行初始化,默认的获取所有的信息

?
protectedvoidPage_Load(objectsender, EventArgs e)
{
if(!IsPostBack)//首次进行该页时,页面初始化
{
RecordsCount = GetRecordsCount("default");//默认信息总数
PagesCount = RecordsCount / PAGESIZE + OverPage();//默认的页总数
ViewState["PagesCount"] = RecordsCount / PAGESIZE - ModPage();//保存末页索引,比页总数小1
ViewState["PageIndex"] = 0;//保存页面初始索引从0开始
ViewState["JumpPages"] = PagesCount;
//保存页总数,跳页时判断用户输入数是否超出页码范围
//显示lbPageCount、lbRecordCount的状态
lbPageCount.Text = PagesCount.ToString();
lbRecordCount.Text = RecordsCount.ToString();
//判断跳页文本框失效
if(RecordsCount <= 10)
{
GotoPage.Enabled = false;
}
GridViewDataBind("default");//调用数据绑定函数TDataBind()进行数据绑定运算
}
}

7、PagerBtnCommand_OnClick:该方法主要用来处理设计视图页的“首页”、“下一页”,“上一页”,“尾页”,“查询”按钮的Click事件,主要通过不同按钮的CommandName属性来分别处理,需要在前台为每一个按钮相应的CommandName属性赋值,如果用户点击的是“查询”按钮,这个时候需要对查询的Sql语句进行重写,加入过滤条件,即用户输入的查询的条件

?
/// <summary>
/// 页面按钮Click处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protectedvoidPagerBtnCommand_OnClick(objectsender, EventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
//从ViewState中读取页码值保存到CurrentPage变量中进行参数运算
Pages = (int)ViewState["PagesCount"];//从ViewState中读取总页参数运算
Button btn = sender asButton;
stringsqlResult="default";
if(btn != null)
{
stringcmd = btn.CommandName;
switch(cmd)//根据不同的CommandName做出不同的处理
{
case"next":
CurrentPage++;
break;
case"prev":
CurrentPage--;
break;
case"last":
CurrentPage = Pages;
break;
case"search":
if(!string.IsNullOrEmpty(SearchName.Text))
{
RecordsCount = GetRecordsCount("select count(*) from p_user where username like '"+ SearchName.Text + "%'");//获取过滤后的总记录数
PagesCount = RecordsCount / PAGESIZE + OverPage();//该变量为页总数
ViewState["PagesCount"] = RecordsCount / PAGESIZE - ModPage();//该变量为末页索引,比页总数小1
ViewState["PageIndex"] = 0;//保存一个为0的页面索引值到ViewState,页面索引从0开始
ViewState["JumpPages"] = PagesCount;
//保存PageCount到ViewState,跳页时判断用户输入数是否超出页码范围
//显示lbPageCount、lbRecordCount的状态
lbPageCount.Text = PagesCount.ToString();
lbRecordCount.Text = RecordsCount.ToString();
//判断跳页文本框失效
if(RecordsCount <= 10)
GotoPage.Enabled = false;
sqlResult = "Select Top "+ PAGESIZE + "user_serialid,username,displayname,orgcode,orgname from p_user where user_serialid not in(select top "+ PAGESIZE * CurrentPage + " user_serialid from p_user order by user_serialid asc) and username like '"+ SearchName.Text + "%' order by user_serialid asc";
}
else
{
Response.Write("请输入您所要查找的用户姓名!");
}
break;
case"jump":
JumpPage = (int)ViewState["JumpPages"];
//从ViewState中读取可用页数值保存到JumpPage变量中
//判断用户输入值是否超过可用页数范围值
if(Int32.Parse(GotoPage.Text) > JumpPage || Int32.Parse(GotoPage.Text) <= 0)
Response.Write("<script>alert('页码范围越界!')</script>");
else
{
intInputPage = Int32.Parse(GotoPage.Text.ToString()) - 1;
//转换用户输入值保存在int型InputPage变量中
ViewState["PageIndex"] = InputPage;
CurrentPage = InputPage;
//写入InputPage值到ViewState["PageIndex"]中
sqlResult = "Select Top "+ PAGESIZE + "user_serialid,username,displayname,orgcode,orgname from p_user where user_serialid not in(select top "+ PAGESIZE * CurrentPage + " user_serialid from p_user order by user_serialid asc) and username like '"+ SearchName.Text + "%' order by user_serialid asc";
}
break;
default:
CurrentPage = 0;
break;
}
ViewState["PageIndex"] = CurrentPage;
//将运算后的CurrentPage变量再次保存至ViewState
GridViewDataBind(sqlResult);//调用数据绑定函数TDataBind()
}
}

8、btn_Reset_Click:该方法主要用来进行重置,用户完成一次查询之后,需要重置,才能进行下一次的查询操作

?
protectedvoidbtnReset_Click(objectsender, EventArgs e)
(
RecordsCount = GetRecordsCount("default");//默认信息总数
PagesCount = RecordsCount / PAGESIZE + OverPage();//默认的页总数
ViewState["PagesCount"] = RecordsCount / PAGESIZE - ModPage();//保存末页索引,比页总数小1
ViewState["PageIndex"] = 0;//保存页面初始索引从0开始
ViewState["JumpPages"] = PagesCount;
//保存页总数,跳页时判断用户输入数是否超出页码范围
//显示lbPageCount、lbRecordCount的状态
lbPageCount.Text = PagesCount.ToString();
lbRecordCount.Text = RecordsCount.ToString();
//判断跳页文本框失效
if(RecordsCount <= 10)
{
GotoPage.Enabled = false;
}
GridViewDataBind("default");//调用数据绑定函数TDataBind()进行数据绑定运算
}

这里的高效分页方法主要用的是select top 10 Id ,Name from tb where Id not in (select top 10*N from tb order by Id asc) order by Id asc

示例中的N代表的是页数,之前原子里也有很多关于高效分页的讨论,这里就不再多说了,我个人觉得这个分页语句效果还不错,当然除此之外还有row_number()函数分页、select Max() 分页等等方法,之前也有总结过,有兴趣的朋友可以看一下我之前写的ListView和Repeater高效分页这篇文章,里边讲述的也很详细,只要你一步一步的照着去操练应该问题不大的。

这里需要解释一下的是为什么没有有数据绑定控件直接进行绑定,因为在sharepoint2010项目里边它支持的数据源控件只有XMLDataSource,所以就只有自己动手实现了。

好了今天就到这里了,希望能给大家带来些帮助吧!还请多多指教!

分享到:
评论

相关推荐

    GridView高效分页和搜索功能的实现代码

    本文将介绍如何在GridView中实现高效分页和搜索功能。 首先,高效分页的关键在于不一次性加载所有数据,而是根据当前页数只从数据库获取对应页的数据。这通常通过SQL查询中的OFFSET和FETCH或TOP关键字来实现,或者...

    GridView不分页情况下的筛选功能

    本主题将深入探讨如何在GridView控件中实现不分页情况下的筛选功能,这对于用户在大量数据中查找特定信息至关重要。 首先,我们需要了解GridView的基本结构。GridView控件默认会按照数据源中的数据量进行分页,但...

    GridView分页效果

    在本示例中,"GridView分页效果"指的是在GridView中实现类似翻页的功能,以便在有限的屏幕空间内高效浏览大量内容,如图片和文字。 在Android中,GridView默认并不直接支持分页,但可以通过一些技巧来模拟分页效果...

    GridView 分页,申请例子

    总的来说,GridView的分页功能使得大量数据的展示变得更加高效,提高了用户体验。通过灵活配置,你可以根据项目需求调整分页样式和行为。在实际开发中,记得结合良好的编程实践,确保代码的可读性和维护性。

    ASP.NET GRIDVIEW 分页控件

    在这个案例中,我们关注的是"GridView分页控件",一个自定义实现的组件,允许用户轻松地在大量数据之间导航。 分页是提高用户体验的关键特性,尤其是在处理大数据集时。默认的ASP.NET GridView控件提供了内置的分页...

    GridView+Jquery实现的TreeGrid

    "GridView+Jquery实现的TreeGrid"是一种高效且灵活的数据呈现方式,它结合了ASP.NET的GridView控件与jQuery库,用于创建具有层级结构的表格,即TreeGrid。这种技术允许用户在不刷新整个页面的情况下,动态地加载和...

    jquery+ashx无刷新GridView数据显示插件(实现分页、排序、过滤功能.doc

    总的来说,这个jQuery+ASHX的无刷新GridView插件提供了一种高效的方法来实现动态数据展示,提高了Web应用的交互性。如果你有改进的建议或想要了解更多细节,可以查阅提供的下载链接来查看完整代码。

    GridView分页,编辑,删除,查看详情

    在本主题中,我们将深入探讨GridView如何实现分页、编辑、删除以及查看详情功能,这些都是在Web应用开发中处理大量数据时不可或缺的功能。 **一、GridView分页** 1. **启用分页**:在GridView控件中,通过设置`...

    asp分页,asp分页仿百度,asp分页大全

    4. 分页控件:使用ASP.NET的GridView或DataGrid控件,它们内置了分页功能,但在ASP中需要找到类似第三方控件或者自定义实现。 五、仿百度分页 仿百度分页通常指的是在样式上模仿百度的分页样式,包括页码展示、跳转...

    Gridview 控件案例教程

    - 查找:虽然GridView本身不提供查找功能,但可以通过添加搜索文本框和处理搜索事件来实现。 4. 分页功能: 要启用分页,需要设置GridView的PageSize属性,确定每页显示的记录数。同时,启用Paging属性,如`...

    .netpage分页多功能实现及实例demo

    在ASP.NET中,有许多内置的分页控件,如SqlDataSource的Paging属性、GridView和ListView等控件的AllowPaging属性。然而,这些默认的分页控件功能相对简单,可能无法满足所有复杂场景的需求。因此,开发者常常需要...

    在线图书购买网站GridView绑定数据进行显示

    总结来说,"在线图书购买网站GridView绑定数据进行显示"涉及到的关键知识点包括:ASP.NET GridView控件的使用、数据库查询与数据绑定、自动分页实现、以及可能的排序和搜索功能。这些技术都是构建高效、用户友好的...

    AJAX 常用案例 gridview updatepanel

    - **分页控件**:在GridView或其他数据展示控件中,使用AJAX实现分页功能,点击分页按钮时仅加载所需数据,而不是整个页面。 - **无刷新更新**:如在GridView中编辑一行数据后,使用AJAX提交更改并更新表格,保持...

    ASPxGridview主从表

    此外,还可以通过编程实现更多的交互效果,比如分页、排序、过滤和搜索功能。 6. 性能优化:在处理大量数据时,为了提高性能,可以使用分页、懒加载(延迟加载)从表数据等技术。当用户滚动到主表的特定行时,才...

    Asp.net下Gridview的72种用法

    19. 搜索过滤:添加TextBox和Button,实现客户端或服务器端的搜索过滤功能。 20. 数据导出:可以将GridView的数据导出为Excel、CSV或其他格式,方便用户处理。 21. 行事件扩展:如RowCreated、RowDeleting等,这些...

    精致的GridView控件源码

    通过使用GridView,开发者可以轻松地创建动态、交互式的表格,包括排序、分页和编辑功能。在默认情况下,GridView会根据数据源自动创建列,但也可以手动配置列以实现更复杂的布局。 这款精致的GridView控件源码特别...

    ASP.NET:gridview和detailsview的组合应用

    GridView控件主要用于显示表格形式的数据,它可以轻松地从数据库或其他数据源绑定数据,并提供排序、分页、筛选和编辑等功能。在描述中提到的"在GridView中添加记录",意味着可以通过GridView的内置编辑功能让用户...

    JQGrid 分页

    JQGrid是一款强大的jQuery插件,用于创建交互式的表格,其功能丰富,包括数据的分页、排序、搜索以及编辑等。在这个特定的"JQGrid分页"项目中,我们关注的是如何实现数据的分页功能,这在处理大量数据时尤为重要,...

Global site tag (gtag.js) - Google Analytics