前言:
公司項目開發(fā),上周的任務是做基礎數(shù)據(jù)的管理。在Sharepoint2010里邊內(nèi)嵌asp.net的aspx頁,遇到了各種各樣奇葩的問題,因為之前對sharepoint只是有一些了解,但是沒有設計到具體的編程工作,這一次算是初次接觸吧。其中有一部分基礎數(shù)據(jù)數(shù)據(jù)量很大,大致有十多萬,因為是對基礎數(shù)據(jù)的維護,所以還需要對數(shù)據(jù)進行列表展示,增刪改查什么的,大家都知道Asp.net里邊的GridView有自帶的分頁,但是,那個分頁對于少量的數(shù)據(jù)還好,對于這種數(shù)十萬的數(shù)據(jù)量而言,這種分頁方式簡直就是災難。網(wǎng)上關(guān)于GridView高效分頁的東西有很多,找了一個自己改了改。用著效果還不錯,和大家分享一下。
這是分頁的效果圖

下邊就講一下具體的實現(xiàn),首先聲明,這個東西是不是我原創(chuàng)的,只是在此基礎上進行了修飾和完善。希望能給各位有所啟發(fā)。
一、前臺布局
復制代碼 代碼如下:
div>
div id="main">
div id="search">
table>
tr>
td>
asp:Label ID="lb" runat="server" Text="姓名">/asp:Label>/td>
td>
asp:TextBox ID="SearchName" runat="server">/asp:TextBox>
/td>
td>
asp:Button ID="btnSearch" runat="server" Text="查詢" onclick="PagerBtnCommand_OnClick" CommandName="search" />
/td>
td>
asp:Button ID="btnReset" runat="server" Text="重置" onclick="btnReset_Click" />
/td>
/tr>
/table>
/div>
div id="gridView">
asp:GridView ID="UserGridView" runat="server" AutoGenerateColumns="false">
Columns>
asp:TemplateField HeaderText="用戶名">
ItemTemplate>
asp:Label ID="UserName" runat="server" Text='%#Eval("username") %>'>/asp:Label>
/ItemTemplate>
/asp:TemplateField>
asp:TemplateField HeaderText="單位名稱">
ItemTemplate>
asp:Label ID="DisplayName" runat="server" Text='%#Eval("displayname") %>'>/asp:Label>
/ItemTemplate>
/asp:TemplateField>
asp:TemplateField HeaderText="組織編碼">
ItemTemplate>
asp:Label ID="OrgCode" runat="server" Text='%#Eval("orgcode") %>'>/asp:Label>
/ItemTemplate>
/asp:TemplateField>
asp:TemplateField HeaderText="組織名稱">
ItemTemplate>
asp:Label ID="OrgName" runat="server" Text='%#Eval("orgname") %>'>/asp:Label>
/ItemTemplate>
/asp:TemplateField>
/Columns>
/asp:GridView>
/div>
div id="page">
table>
tr>
td>
asp:Label ID="lbcurrentpage1" runat="server" Text="當前頁:">/asp:Label>
asp:Label ID="lbCurrentPage" runat="server" Text="">/asp:Label>
asp:Label ID="lbFenGe" runat="server" Text="/">/asp:Label>
asp:Label ID="lbPageCount" runat="server" Text="">/asp:Label>
/td>
td>
asp:Label ID="recordscount" runat="server" Text="總條數(shù):">/asp:Label>
asp:Label ID="lbRecordCount" runat="server" Text="">/asp:Label>
/td>
td>
asp:Button ID="Fistpage" runat="server" CommandName="" Text="首頁" OnClick="PagerBtnCommand_OnClick" />
asp:Button ID="Prevpage" runat="server" CommandName="prev" Text="上一頁"
OnClick="PagerBtnCommand_OnClick" />
asp:Button ID="Nextpage" runat="server" CommandName="next" Text="下一頁" OnClick="PagerBtnCommand_OnClick" />
asp:Button ID="Lastpage" runat="server" CommandName="last" Text="尾頁"
key="last" OnClick="PagerBtnCommand_OnClick" />
/td>
td>
asp:Label ID="lbjumppage" runat="server" Text="跳轉(zhuǎn)到第">/asp:Label>
asp:TextBox ID="GotoPage" runat="server" Width="25px">/asp:TextBox>
asp:Label ID="lbye" runat="server" Text="頁">/asp:Label>
asp:Button ID="Jump" runat="server" Text="跳轉(zhuǎn)" CommandName="jump" OnClick="PagerBtnCommand_OnClick" />
/td>
/tr>
/table>
/div>
/div>
/div>
布局的效果如下:

二、后臺的代碼實現(xiàn)
我會一點一點向大家講解清楚,這個分頁的原理
Members:這里主要定義幾個全局的變量,主要用來記錄信息的數(shù)量、頁的數(shù)量和當前頁
復制代碼 代碼如下:
#region Members
const int PAGESIZE = 10;//每頁顯示信息數(shù)量
int PagesCount, RecordsCount;//記錄總頁數(shù)和信息總條數(shù)
int CurrentPage, Pages, JumpPage;//當前頁,信息總頁數(shù)(用來控制按鈕失效),跳轉(zhuǎn)頁碼
const string COUNT_SQL = "select count(*) from p_user";
#endregion
Methods:
1、GetRecordsCount:該方法主要用來獲取當前信息的總數(shù),有一個sqlSearch參數(shù),默認的為default,即初始化頁面時,查詢所有信息的總條數(shù),當用戶輸入要搜索的用戶名進行檢索時,獲取符合用戶檢索條件的信息的總條數(shù)
復制代碼 代碼如下:
/// summary>
/// 獲取信息總數(shù)
/// /summary>
/// param name="sqlSearch">/param>
/// returns>/returns>
public static int GetRecordsCount(string sqlRecordsCount)
{
string sqlQuery;
if (sqlRecordsCount == "default")
{
sqlQuery = COUNT_SQL;
}
else
{
sqlQuery = sqlRecordsCount;
}
int RecordCount = 0;
SqlCommand cmd = new SqlCommand(sqlQuery, Conn());
RecordCount = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Connection.Close();
return RecordCount;
}
2、OverPage:該方法主要用來計算剩余頁,當前設置的為每頁顯示10條數(shù)據(jù),如何符合條件的數(shù)據(jù)有11條,則要顯示2頁
復制代碼 代碼如下:
/// summary>
/// 計算余頁
/// /summary>
/// returns>/returns>
public int OverPage()
{
int pages = 0;
if (RecordsCount % PAGESIZE != 0)
pages = 1;
else
pages = 0;
return pages;
}
3、ModPage:該方法也是用計算余頁,主要用來防止SQL執(zhí)行溢出
復制代碼 代碼如下:
/// summary>
/// 計算余頁,防止SQL語句執(zhí)行時溢出查詢范圍
/// /summary>
/// returns>/returns>
public int ModPage()
{
int pages = 0;
if (RecordsCount % PAGESIZE == 0 RecordsCount != 0)
pages = 1;
else
pages = 0;
return pages;
}
4、Conn:該方法用來創(chuàng)建數(shù)據(jù)連接對象,在使用的時候只需改成自己的數(shù)據(jù)庫名即可
復制代碼 代碼如下:
/// summary>
/// 數(shù)據(jù)連接對象
/// /summary>
/// returns>/returns>
public static SqlConnection Conn()
{
SqlConnection conn = new SqlConnection("data source=.;initial catalog=DB_GSL_ZCW;Integrated Security=true");
conn.Open();
return conn;
}
5、GridViewDataBind:該方法主要用來數(shù)據(jù)綁定,如果傳入的參數(shù)為default則,默認的綁定所有的數(shù)據(jù),否則,則綁定過濾過的數(shù)據(jù)
復制代碼 代碼如下:
/// summary>
/// GridView數(shù)據(jù)綁定,根據(jù)傳入?yún)?shù)的不同,進行不同方式的查詢,
/// /summary>
/// param name="sqlSearch">/param>
private void GridViewDataBind(string sqlSearch)
{
CurrentPage = (int)ViewState["PageIndex"];
//從ViewState中讀取頁碼值保存到CurrentPage變量中進行按鈕失效運算
Pages = (int)ViewState["PagesCount"];
//從ViewState中讀取總頁參數(shù)進行按鈕失效運算
//判斷四個按鈕(首頁、上一頁、下一頁、尾頁)狀態(tài)
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 = new DataSet();
string sqlResult;
//根據(jù)傳入?yún)?shù)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 = new SqlDataAdapter(sqlResult, Conn());
sqlAdapter.Fill(ds, "Result");
UserGridView.DataSource = ds.Tables["Result"].DefaultView;
UserGridView.DataBind();
//顯示Label控件lbCurrentPaget和文本框控件GotoPage狀態(tài)
lbCurrentPage.Text = (CurrentPage + 1).ToString();
GotoPage.Text = (CurrentPage + 1).ToString();
sqlAdapter.Dispose();
}
6、Page_Load:頁面加載函數(shù),主要是在首次進入頁面時,進行初始化,默認的獲取所有的信息
復制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//首次進行該頁時,頁面初始化
{
RecordsCount = GetRecordsCount("default");//默認信息總數(shù)
PagesCount = RecordsCount / PAGESIZE + OverPage();//默認的頁總數(shù)
ViewState["PagesCount"] = RecordsCount / PAGESIZE - ModPage();//保存末頁索引,比頁總數(shù)小1
ViewState["PageIndex"] = 0;//保存頁面初始索引從0開始
ViewState["JumpPages"] = PagesCount;
//保存頁總數(shù),跳頁時判斷用戶輸入數(shù)是否超出頁碼范圍
//顯示lbPageCount、lbRecordCount的狀態(tài)
lbPageCount.Text = PagesCount.ToString();
lbRecordCount.Text = RecordsCount.ToString();
//判斷跳頁文本框失效
if (RecordsCount = 10)
{
GotoPage.Enabled = false;
}
GridViewDataBind("default");//調(diào)用數(shù)據(jù)綁定函數(shù)TDataBind()進行數(shù)據(jù)綁定運算
}
}
7、PagerBtnCommand_OnClick:該方法主要用來處理設計視圖頁的“首頁”、“下一頁”,“上一頁”,“尾頁”,“查詢”按鈕的Click事件,主要通過不同按鈕的CommandName屬性來分別處理,需要在前臺為每一個按鈕相應的CommandName屬性賦值,如果用戶點擊的是“查詢”按鈕,這個時候需要對查詢的Sql語句進行重寫,加入過濾條件,即用戶輸入的查詢的條件
復制代碼 代碼如下:
/// summary>
/// 頁面按鈕Click處理
/// /summary>
/// param name="sender">/param>
/// param name="e">/param>
protected void PagerBtnCommand_OnClick(object sender, EventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
//從ViewState中讀取頁碼值保存到CurrentPage變量中進行參數(shù)運算
Pages = (int)ViewState["PagesCount"];//從ViewState中讀取總頁參數(shù)運算
Button btn = sender as Button;
string sqlResult="default";
if (btn != null)
{
string cmd = btn.CommandName;
switch (cmd)//根據(jù)不同的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 + "%'");//獲取過濾后的總記錄數(shù)
PagesCount = RecordsCount / PAGESIZE + OverPage();//該變量為頁總數(shù)
ViewState["PagesCount"] = RecordsCount / PAGESIZE - ModPage();//該變量為末頁索引,比頁總數(shù)小1
ViewState["PageIndex"] = 0;//保存一個為0的頁面索引值到ViewState,頁面索引從0開始
ViewState["JumpPages"] = PagesCount;
//保存PageCount到ViewState,跳頁時判斷用戶輸入數(shù)是否超出頁碼范圍
//顯示lbPageCount、lbRecordCount的狀態(tài)
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中讀取可用頁數(shù)值保存到JumpPage變量中
//判斷用戶輸入值是否超過可用頁數(shù)范圍值
if(Int32.Parse(GotoPage.Text) > JumpPage || Int32.Parse(GotoPage.Text) = 0)
Response.Write("script>alert('頁碼范圍越界!')/script>");
else
{
int InputPage = Int32.Parse(GotoPage.Text.ToString()) - 1;
//轉(zhuǎn)換用戶輸入值保存在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);//調(diào)用數(shù)據(jù)綁定函數(shù)TDataBind()
}
}
8、btn_Reset_Click:該方法主要用來進行重置,用戶完成一次查詢之后,需要重置,才能進行下一次的查詢操作
復制代碼 代碼如下:
protected void btnReset_Click(object sender, EventArgs e)
(
RecordsCount = GetRecordsCount("default");//默認信息總數(shù)
PagesCount = RecordsCount / PAGESIZE + OverPage();//默認的頁總數(shù)
ViewState["PagesCount"] = RecordsCount / PAGESIZE - ModPage();//保存末頁索引,比頁總數(shù)小1
ViewState["PageIndex"] = 0;//保存頁面初始索引從0開始
ViewState["JumpPages"] = PagesCount;
//保存頁總數(shù),跳頁時判斷用戶輸入數(shù)是否超出頁碼范圍
//顯示lbPageCount、lbRecordCount的狀態(tài)
lbPageCount.Text = PagesCount.ToString();
lbRecordCount.Text = RecordsCount.ToString();
//判斷跳頁文本框失效
if (RecordsCount = 10)
{
GotoPage.Enabled = false;
}
GridViewDataBind("default");//調(diào)用數(shù)據(jù)綁定函數(shù)TDataBind()進行數(shù)據(jù)綁定運算
}
這里的高效分頁方法主要用的是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代表的是頁數(shù),之前原子里也有很多關(guān)于高效分頁的討論,這里就不再多說了,我個人覺得這個分頁語句效果還不錯,當然除此之外還有row_number()函數(shù)分頁、select Max() 分頁等等方法,之前也有總結(jié)過,有興趣的朋友可以看一下我之前寫的ListView和Repeater高效分頁這篇文章,里邊講述的也很詳細,只要你一步一步的照著去操練應該問題不大的。
這里需要解釋一下的是為什么沒有有數(shù)據(jù)綁定控件直接進行綁定,因為在sharepoint2010項目里邊它支持的數(shù)據(jù)源控件只有XMLDataSource,所以就只有自己動手實現(xiàn)了。
好了今天就到這里了,希望能給大家?guī)硇椭?!還請多多指教!
您可能感興趣的文章:- asp.net gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁
- asp.net中的GridView分頁問題
- Android入門之ActivityGroup+GridView實現(xiàn)Tab分頁標簽的方法
- GridView分頁的實現(xiàn)以及自定義分頁樣式功能實例
- Android中實現(xiàn)多行、水平滾動的分頁的Gridview實例源碼
- Asp.net GridView使用大全(分頁實現(xiàn))
- GridView分頁的實現(xiàn)(通用分頁模板)
- AspNetPager+GridView實現(xiàn)分頁的實例代碼
- asp.net中g(shù)ridview的查詢、分頁、編輯更新、刪除的實例代碼
- 解析GridView自帶分頁及與DropDownList結(jié)合使用