国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

為DataGrid自定義分頁添加自定義導航和分頁信息

2024-07-21 02:23:10
字體:
來源:轉載
供稿:網友

在上一篇文章中我講到了對datagrid實行自定義分頁,這可以避免為了顯示一頁數據而獲取整個數據記錄集,從而提高分頁效率,不過使用的導航還是datagrid自帶的數字連接或簡單的上一頁,下一頁,而且看不到總頁數、總記錄數之類的信息。下面就為他增加我們所需要的部分。

先來看看修改后的分頁顯示,截圖如下:



(圖一)

使用的數據源同上一篇文章(asp.net中datagrid控件的自定義分頁)相同,都是訪問northwind庫,為了獨立開來這里還是把存儲過程列了一下,

create procedure [getcustomersdatapage]

@pageindex int,

@pagesize int,

@recordcount int out,

@pagecount int out

as

select @recordcount = count(*) from customers

set @pagecount = ceiling(@recordcount * 1.0 / @pagesize)

declare @sqlstr nvarchar(1000)

if @pageindex = 0 or @pagecount <= 1

set @sqlstr =n'select top '+str( @pagesize )+

' customerid, companyname,address,phone from customers order by customerid desc

else if @pageindex = @pagecount - 1

set @sqlstr =n' select * from ( select top '+str( @recordcount - @pagesize * @pageindex )+

' customerid, companyname,address,phone from customers order by customerid asc ) temptable order by customerid desc'

else

set @sqlstr =n' select top '+str( @pagesize )+' * from ( select top '+str( @recordcount - @pagesize * @pageindex )+

' customerid, companyname,address,phone from customers order by customerid asc ) temptable order by customerid desc'



exec (@sqlstr)

go



下面就就把代碼貼了一下,

aspx文件代碼如下:

<%@ page language="c#" codebehind="datagridcustompaging.aspx.cs" autoeventwireup="false" inherits="zz.aspnetpaging.datagridcustompaging" %>

<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >

<html>

<head>

<title>datagridpaging</title>

<meta content="microsoft visual studio .net 7.1" name="generator">

<meta content="c#" name="code_language">

<meta content="javascript" name="vs_defaultclientscript">

<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">

</head>

<body>

<form id="form1" method="post" runat="server">

<table id="table1" style="font-size: 9pt" cellspacing="1" cellpadding="1" width="450" align="center"

border="1">

<tr>

<td><asp:datagrid id="datagrid1" runat="server" allowpaging="true" allowcustompaging="true" width="100%">

<footerstyle font-size="9pt"></footerstyle>

<headerstyle font-size="9pt"></headerstyle>

<pagerstyle visible="false" font-size="9pt" mode="numericpages"></pagerstyle>

</asp:datagrid></td>

</tr>

<tr>

<td>

<table id="table2" style="font-size: 9pt" cellspacing="1" cellpadding="1" width="100%"

align="center" border="1">

<tr>

<td style="width: 150px"><asp:linkbutton id="lbtnfirst" runat="server" commandname="first">首頁</asp:linkbutton>&nbsp;

<asp:linkbutton id="lbtnprev" runat="server" commandname="prev">上一頁</asp:linkbutton>&nbsp;

<asp:linkbutton id="lbtnnext" runat="server" commandname="next">下一頁</asp:linkbutton>&nbsp;

<asp:linkbutton id="lbtnlast" runat="server" commandname="last">尾頁</asp:linkbutton></td>

<td>第<asp:literal id="ltlpageindex" runat="server"></asp:literal>頁&nbsp; 共<asp:literal id="ltlpagecount" runat="server"></asp:literal>頁&nbsp;

每頁<asp:literal id="ltlpagesize" runat="server"></asp:literal>條&nbsp; 共<asp:literal id="ltlrecordcount" runat="server"></asp:literal>條&nbsp;

</td>

</tr>

</table>

</td>

</tr>

</table>

</form>

</body>

</html>



aspx.cs文件代碼如下:

using system;

using system.collections;

using system.componentmodel;

using system.data;

using system.drawing;

using system.web;

using system.web.sessionstate;

using system.web.ui;

using system.web.ui.webcontrols;

using system.web.ui.htmlcontrols;

using system.data.sqlclient;

using system.configuration;



namespace zz.aspnetpaging

{

public class datagridcustompaging : system.web.ui.page

{

private int pagecount;

private int recordcount;



protected system.web.ui.webcontrols.linkbutton lbtnfirst;

protected system.web.ui.webcontrols.linkbutton lbtnprev;

protected system.web.ui.webcontrols.linkbutton lbtnnext;

protected system.web.ui.webcontrols.linkbutton lbtnlast;

protected system.web.ui.webcontrols.literal ltlpageindex;

protected system.web.ui.webcontrols.literal ltlpagecount;

protected system.web.ui.webcontrols.literal ltlpagesize;

protected system.web.ui.webcontrols.literal ltlrecordcount;

protected system.web.ui.webcontrols.datagrid datagrid1;



private void page_load(object sender, system.eventargs e)

{

if(!page.ispostback)

{

datagriddatabind();

}

}



//綁定數據

private void datagriddatabind()

{

dataset ds = getcustomersdata(pageindex,pagesize,ref recordcount,ref pagecount);

this.datagrid1.virtualitemcount = recordcount;

this.datagrid1.datasource = ds;

this.datagrid1.databind();

setpagingstate();

}



#region web 窗體設計器生成的代碼

override protected void oninit(eventargs e)

{

initializecomponent();

base.oninit(e);

}



private void initializecomponent()

{

this.lbtnfirst.click += new system.eventhandler(this.lbtnnavigation_click);

this.lbtnprev.click += new system.eventhandler(this.lbtnnavigation_click);

this.lbtnnext.click += new system.eventhandler(this.lbtnnavigation_click);

this.lbtnlast.click += new system.eventhandler(this.lbtnnavigation_click);

this.load += new system.eventhandler(this.page_load);

}

#endregion



private static dataset getcustomersdata(int pageindex,int pagesize,ref int recordcount,ref int pagecount)

{

string connstring = configurationsettings.appsettings["connstring"];

sqlconnection conn = new sqlconnection(connstring);

sqlcommand comm = new sqlcommand("getcustomersdatapage",conn);

comm.parameters.add(new sqlparameter("@pageindex",sqldbtype.int));

comm.parameters[0].value = pageindex;

comm.parameters.add(new sqlparameter("@pagesize",sqldbtype.int));

comm.parameters[1].value = pagesize;

comm.parameters.add(new sqlparameter("@recordcount",sqldbtype.int));

comm.parameters[2].direction = parameterdirection.output;

comm.parameters.add(new sqlparameter("@pagecount",sqldbtype.int));

comm.parameters[3].direction = parameterdirection.output;

comm.commandtype = commandtype.storedprocedure;

sqldataadapter dataadapter = new sqldataadapter(comm);

dataset ds = new dataset();

dataadapter.fill(ds);

recordcount = (int)comm.parameters[2].value;

pagecount = (int)comm.parameters[3].value;

return ds;

}



private void lbtnnavigation_click(object sender, system.eventargs e)

{

linkbutton btn = (linkbutton)sender;

switch(btn.commandname)

{

case "first":

pageindex = 0;

break;

case "prev"://if( pageindex > 0 )

pageindex = pageindex - 1;

break;

case "next"://if( pageindex < pagecount -1)

pageindex = pageindex + 1;

break;

case "last":

pageindex = pagecount - 1;

break;

}

datagriddatabind();

}



/// <summary>

/// 控制導航按鈕或數字的狀態

/// </summary>

public void setpagingstate()

{

if( pagecount <= 1 )//( recordcount <= pagesize )//小于等于一頁

{

this.lbtnfirst.enabled = false;

this.lbtnprev.enabled = false;

this.lbtnnext.enabled = false;

this.lbtnlast.enabled = false;

}

else //有多頁

{

if( pageindex == 0 )//當前為第一頁

{

this.lbtnfirst.enabled = false;

this.lbtnprev.enabled = false;

this.lbtnnext.enabled = true;

this.lbtnlast.enabled = true;

}

else if( pageindex == pagecount - 1 )//當前為最后頁

{

this.lbtnfirst.enabled = true;

this.lbtnprev.enabled = true;

this.lbtnnext.enabled = false;

this.lbtnlast.enabled = false;

}

else //中間頁

{

this.lbtnfirst.enabled = true;

this.lbtnprev.enabled = true;

this.lbtnnext.enabled = true;

this.lbtnlast.enabled = true;

}

}



this.ltlpagesize.text = pagesize.tostring();

this.ltlrecordcount.text = recordcount.tostring();

if(recordcount == 0)

{

this.ltlpagecount.text = "0";

this.ltlpageindex.text = "0";

}

else

{

this.ltlpagecount.text = pagecount.tostring();

this.ltlpageindex.text = (pageindex + 1).tostring();

}

}





public int pagecount

{

get

{

return this.datagrid1.pagecount;

}

}



public int pagesize

{

get

{

return this.datagrid1.pagesize;

}

}



public int pageindex

{

get

{

return this.datagrid1.currentpageindex;

}

set

{

this.datagrid1.currentpageindex = value;

}

}



public int recordcount

{

get

{

return recordcount;

}

}

}

}



上面的代碼比較簡單,也就不用分析了,如果有什么好的建議或有問題可以在blog上留言,很高興同大家交流。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沅江市| 毕节市| 长丰县| 永春县| 安塞县| 临城县| 宽甸| 荆门市| 宜良县| 琼结县| 清水县| 浦东新区| 乡城县| 梅州市| 邓州市| 濮阳县| 调兵山市| 南靖县| 青神县| 塘沽区| 昆明市| 介休市| 岳西县| 都江堰市| 黄大仙区| 建宁县| 井研县| 甘肃省| 黄龙县| 新河县| 伊宁县| 上虞市| 磐安县| 永嘉县| 宜昌市| 崇礼县| 竹溪县| 萨嘎县| 绥阳县| 汝州市| 黄梅县|