轉(zhuǎn)帖|其它|編輯:郝浩|2011-03-01 13:55:57.000|閱讀 487 次
概述:大家都知道asp.net中的Gridview。datalist等都可以自定義分頁(yè),但是當(dāng)你翻頁(yè)的時(shí)候,數(shù)據(jù)表中的所有數(shù)據(jù)都會(huì)加載到內(nèi)存,重新綁定,當(dāng)然要是數(shù)據(jù)量小的話,這是可以的,我們也很樂(lè)意用,原因簡(jiǎn)單因?yàn)榉奖?,但是要是?shù)據(jù)量是999999999999……,在信息爆炸的這個(gè)時(shí)代海量數(shù)據(jù)是經(jīng)常的時(shí),那么這些控件自帶的分頁(yè)就顯得有些棘手。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
大家都知道asp.net中的Gridview。datalist等都可以自定義分頁(yè),但是當(dāng)你翻頁(yè)的時(shí)候,數(shù)據(jù)表中的所有數(shù)據(jù)都會(huì)加載到內(nèi)存,重新綁定,當(dāng)然要是數(shù)據(jù)量小的話,這是可以的,我們也很樂(lè)意用,原因簡(jiǎn)單因?yàn)榉奖?,但是要是?shù)據(jù)量是999999999999……,在信息爆炸的這個(gè)時(shí)代海量數(shù)據(jù)是經(jīng)常的時(shí),那么這些控件自帶的分頁(yè)就顯得有些棘手。
解決這個(gè)問(wèn)題辦法就是自己動(dòng)手……不多廢話了,看代碼:
1.首先我是用存儲(chǔ)過(guò)程來(lái)解決的,要弄懂這個(gè)問(wèn)題,首先要從存儲(chǔ)過(guò)程下手,代碼如下:
CREATE proc getdataset
@TableList Varchar(200)='*',--搜索表的字段,比如:’id,datatime,job‘,用逗號(hào)隔開(kāi)
@TableName Varchar(30), --搜索的表名
@SelectWhere Varchar(500)='',--搜索條件,這里不用寫(xiě)where,比如:job=’teacher‘and class='2'
@SelectOrderId Varchar(20),--表主鍵字段名。比如:id
@SelectOrder Varchar(200)='', --排序,可以使用多字段排序但主鍵字段必需在最前面。也可以不寫(xiě),比如:order by class asc
@intPageNo int=1, --頁(yè)號(hào)
@intPageSize int=10 ,--每頁(yè)顯示數(shù)
@RecordCount int OUTPUT --總記錄數(shù)(存儲(chǔ)過(guò)程輸出參數(shù))
as
declare @TmpSelect NVarchar(600)
declare @Tmp NVarchar(600)
set nocount on--關(guān)閉計(jì)數(shù)
set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhere
execute sp_executesql
@TmpSelect, --執(zhí)行上面的sql語(yǔ)句
N'@RecordCount int OUTPUT' , --執(zhí)行輸出數(shù)據(jù)的sql語(yǔ)句,output出總記錄數(shù)
@RecordCount OUTPUT
if (@RecordCount = 0) --如果沒(méi)有貼子,則返回零
return 0
/*判斷頁(yè)數(shù)是否正確*/
if (@intPageNo - 1) * @intPageSize > @RecordCount --頁(yè)號(hào)大于總頁(yè)數(shù),返回錯(cuò)誤
return (-1)
set nocount off--打開(kāi)計(jì)數(shù)
if @SelectWhere != ''
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
end
else
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectOrder+') '+@SelectOrder
end
execute sp_executesql @TmpSelect
return(@@rowcount)
GO
其實(shí)代碼也很簡(jiǎn)單,學(xué)編程的人基本上都是懂?dāng)?shù)據(jù)庫(kù)的,這個(gè)存儲(chǔ)過(guò)程估計(jì)不是問(wèn)題。
其他的代碼我都做了解釋,有顏色的那段我沒(méi)有解釋,我在這里解釋一下。其實(shí)也很簡(jiǎn)單,大家來(lái)看:
select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+'
這段代碼的執(zhí)行結(jié)果是什么了,是不是當(dāng)前頁(yè)前面的主鍵的集合啊,現(xiàn)在我們從所有的表中選出主鍵的值不在這個(gè)結(jié)果的之內(nèi)的pagesize個(gè)記錄不就是當(dāng)前頁(yè)的內(nèi)容了嗎?
2.aspx頁(yè)面就不用再將了吧?我這里將代碼寫(xiě)上:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="aa.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="//www.w3.org/1999/xhtml" >
<head runat="server">
<title>無(wú)標(biāo)題頁(yè)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="180px" Width="867px">
<Columns>
<asp:BoundField DataField="job_id" HeaderText="job_id" />
<asp:BoundField DataField="job_desc" HeaderText="job_desc" />
<asp:BoundField DataField="max_lvl" HeaderText="max_lxl" />
</Columns>
</asp:GridView>
</div>
<asp:HyperLink ID="hylfirst" runat="server">金喜正規(guī)買球</asp:HyperLink>
<asp:HyperLink ID="hylprev" runat="server">上一頁(yè)</asp:HyperLink>
<asp:HyperLink ID="hylnext" runat="server">下一頁(yè)</asp:HyperLink>
<asp:HyperLink ID="hylend" runat="server">尾頁(yè)</asp:HyperLink>
第<asp:Label ID="lbRow" runat="server" Text="Label"></asp:Label>頁(yè),
共<asp:Label ID="lbpage" runat="server" Text="Label"></asp:Label>頁(yè),共<asp:Label
ID="lbRecord" runat="server" Text="Label"></asp:Label>條記錄,轉(zhuǎn)到<asp:TextBox ID="txtlink"
runat="server" Width="29px"></asp:TextBox>
頁(yè)<asp:LinkButton ID="link" runat="server" OnClick="link_Click" TabIndex="1">轉(zhuǎn)到</asp:LinkButton>
</form>
</body>
</html>
3.cs頁(yè)面其實(shí)也每頁(yè)什么好講的,也就是一些常用的代碼罷了……我把代碼加上,大家看看,要是有疑問(wèn)的可以回復(fù)我再解釋:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.bind();
}
protected void link_Click(object sender, EventArgs e)
{
int page = Convert.ToInt32(txtlink.Text);
Response.Redirect("aa.aspx?CurrentPage="+page+"");
}
public void bind()
{
int sumPage;
int pageNo = 1;
int pageSize = 3;
if (Request.QueryString["CurrentPage"] == null)
{
pageNo = 1;
}
else
{
pageNo = Int32.Parse(Request.QueryString["CurrentPage"]);
}
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "getdataset";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("@TableList", SqlDbType.VarChar, 200)。Value = "job_id,job_desc,max_lvl";
da.SelectCommand.Parameters.Add("@TableName", SqlDbType.VarChar, 30)。Value = "jobs";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500)。Value = "where d=1";
da.SelectCommand.Parameters.Add("@SelectOrderId", SqlDbType.VarChar, 20)。Value = "job_id";
da.SelectCommand.Parameters.Add("@SelectOrder", SqlDbType.VarChar, 200)。Value = "order by min_lvl asc";
da.SelectCommand.Parameters.Add("@intPageNo", SqlDbType.Int)。Value = pageNo;
da.SelectCommand.Parameters.Add("@intPageSize", SqlDbType.Int)。Value = pageSize;
da.SelectCommand.Parameters.Add("@RecordCount", SqlDbType.Int)。Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.Add("RowCount", SqlDbType.Int)。Direction = ParameterDirection.ReturnValue;
DataSet ds = new DataSet();
da.Fill(ds, "jobs");
GridView1.DataSource = ds;
GridView1.DataBind();
Int32 RecordCount = (Int32)da.SelectCommand.Parameters["@RecordCount"].Value; //求出總記錄數(shù),該值是output出來(lái)的值
Int32 RowCount = (Int32)da.SelectCommand.Parameters["RowCount"].Value; //求出當(dāng)前頁(yè)中的記錄數(shù),在最后一頁(yè)不等于pagesize,
lbRecord.Text = RecordCount.ToString();
lbRow.Text = pageNo.ToString();
sumPage = (Int32)RecordCount / pageSize;
if (RecordCount % pageSize > 0)
{
sumPage = sumPage + 1;
}
lbpage.Text = sumPage.ToString();
if (pageNo > 1)
{
hylfirst.NavigateUrl = "aa.aspx?CurrentPage=1";
hylprev.NavigateUrl = string.Concat("aa.aspx?CurrentPage=", "", pageNo - 1);
}
else
{
hylprev.NavigateUrl = "";
hylfirst.NavigateUrl = "";
hylfirst.Visible = false;
hylprev.Visible = false;
}
if (pageNo < sumPage)
{
hylend.NavigateUrl = string.Concat("aa.aspx?CurrentPage=", "", sumPage);
hylnext.NavigateUrl = string.Concat("aa.aspx?CurrentPage=", "", pageNo + 1);
}
else
{
hylnext.NavigateUrl = "";
hylend.NavigateUrl = "";
hylend.Visible = false;
hylnext.Visible = false;
}
}
}
就這樣吧。要是大家有疑問(wèn),回帖我們?cè)儆懻?,在研?hellip;…
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載