分享
 
 
 

[转帖]DataGrid的自定义分页UserControl

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

PageChange.ascx

================================================================

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="PageChange.ascx.cs" Inherits="Ex_Test.PageChange" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>

<FONT face="宋体">

<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="98%" border="0">

<TR>

<TD align="right"><asp:linkbutton id="FirstPage" runat="server">[首 页]</asp:linkbutton>&nbsp;

<asp:linkbutton id="PrevPage" runat="server">[上一页]</asp:linkbutton>&nbsp;

<asp:linkbutton id="NextPage" runat="server">[下一页]</asp:linkbutton>&nbsp;

<asp:linkbutton id="LastPage" runat="server">[末 页]</asp:linkbutton>&nbsp;

<asp:literal id="Literal1" runat="server" Text="转到第"></asp:literal><asp:textbox id="NewPageIndex" runat="server" Width="31px"></asp:textbox><asp:literal id="Literal2" runat="server" Text="页"></asp:literal>&nbsp;

<asp:button id="NewPageGo" runat="server" Text="Go"></asp:button>&nbsp;</TD>

</TR>

</TABLE>

</FONT>

=======================================================================

PageChange.ascx.cs

===================================================================

namespace Ex_Test

{

using System;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

/// <summary>

/// PageChange 的摘要说明。

/// </summary>

public abstract class PageChange : System.Web.UI.UserControl

{

protected System.Web.UI.WebControls.Button NewPageGo;

protected System.Web.UI.WebControls.Literal Literal2;

protected System.Web.UI.WebControls.TextBox NewPageIndex;

protected System.Web.UI.WebControls.Literal Literal1;

protected System.Web.UI.WebControls.LinkButton LastPage;

protected System.Web.UI.WebControls.LinkButton NextPage;

protected System.Web.UI.WebControls.LinkButton PrevPage;

protected System.Web.UI.WebControls.LinkButton FirstPage;

protected int currentpage;

protected int pagesize;

protected string proc;

protected System.Web.UI.WebControls.DataGrid datagrid;

public int _CurrentPage

{

get

{

return currentpage;

}

set

{

currentpage = value;

}

}

public int _pageSize

{

get

{

return pagesize;

}

set

{

pagesize = value;

}

}

public string _proc

{

get

{

return proc;

}

set

{

proc = value;

}

}

public DataGrid _datagrid

{

get

{

return datagrid;

}

set

{

datagrid = value;

}

}

protected int rowcount;

private void Page_Load(object sender, System.EventArgs e)

{

// 在此处放置用户代码以初始化页面

if(!IsPostBack)

{

using(SqlConnection conn = new SqlConnection("User id=sa;password=admin;server=server-mk;initial catalog=pubs;timeout=90"

)

{

SqlCommand cmd = new SqlCommand("select count(*) as expr1 from authors",conn);

cmd.Connection.Open();

rowcount = (int)cmd.ExecuteScalar();

cmd.Connection.Close();

ViewState["rowscount"] = rowcount;

}

ViewState["currentpage"] = currentpage;

FillGrid(proc,currentpage,pagesize,datagrid);

}

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

/// 设计器支持所需的方法 - 不要使用

/// 代码编辑器修改此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.FirstPage.Click += new System.EventHandler(this.FirstPage_Click);

this.PrevPage.Click += new System.EventHandler(this.PrevPage_Click);

this.NextPage.Click += new System.EventHandler(this.NextPage_Click);

this.LastPage.Click += new System.EventHandler(this.LastPage_Click);

this.NewPageGo.Click += new System.EventHandler(this.NewPageGo_Click);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

private void FillGrid(string proc,int currentpage,int pagesize,DataGrid datagrid)

{

using(SqlConnection conn = new SqlConnection("User id=sa;password=admin;server=server-mk;initial catalog=pubs;timeout=90"

)

{

SqlCommand cmd = new SqlCommand(proc,conn);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@CurrentPage",currentpage);

cmd.Parameters.Add("@PageSize",pagesize);

cmd.Connection.Open();

SqlDataReader sdr = cmd.ExecuteReader();

datagrid.DataSource = sdr;

datagrid.DataBind();

sdr.Close();

cmd.Connection.Close();

}

}

//首页

private void FirstPage_Click(object sender, System.EventArgs e)

{

//disabled首页按钮和上一页按钮

FirstPage.Enabled = false;

PrevPage.Enabled = false;

currentpage = 0;

ViewState["currentpage"] = currentpage;

FillGrid(proc,currentpage,pagesize,datagrid);

//如果不止一页

if((int)ViewState["rowscount"]>((int)ViewState["currentpage"]+1)*pagesize)

{

NextPage.Enabled = true;

}

if((int)ViewState["rowscount"]>((int)ViewState["currentpage"]+1)*pagesize)

{

LastPage.Enabled = true;

}

}

//上一页

private void PrevPage_Click(object sender, System.EventArgs e)

{

NextPage.Enabled = true;

LastPage.Enabled = true;

currentpage = (int)ViewState["currentpage"]-1;

ViewState["currentpage"] = currentpage;

FillGrid(proc,currentpage,pagesize,datagrid);

//如果到首页则disabled首页和上一页按钮

if((int)ViewState["currentpage"]==0)

{

PrevPage.Enabled = false;

FirstPage.Enabled = false;

//return;

}

}

//下一页

private void NextPage_Click(object sender, System.EventArgs e)

{

ViewState["currentpage"] = (int)ViewState["currentpage"]+1;

currentpage = (int)ViewState["currentpage"];

FillGrid(proc,currentpage,pagesize,datagrid);

PrevPage.Enabled = true;

FirstPage.Enabled = true;

//如果已经到了最后一页

if(((int)ViewState["currentpage"]+1)*pagesize>(int)ViewState["rowscount"])

{

NextPage.Enabled = false;

LastPage.Enabled = false;

}

}

//末页

private void LastPage_Click(object sender, System.EventArgs e)

{

LastPage.Enabled = false;

NextPage.Enabled = false;

ViewState["currentpage"] = (int)Math.Ceiling((int)ViewState["rowscount"]/pagesize);

currentpage = (int)ViewState["currentpage"];

FillGrid(proc,currentpage,pagesize,datagrid);

//如果有不止一页的纪录

if((int)ViewState["currentpage"]>1)

{

FirstPage.Enabled = true;

PrevPage.Enabled = true;

}

//如果只有一页的纪录

else

{

FirstPage.Enabled = false;

PrevPage.Enabled = false;

}

}

//跳转

private void NewPage_Go(string i)

{

try

{

int PageIndex = Int32.Parse(i);

if (PageIndex<=0)

{

PageIndex = 0;

}

else

{

if(PageIndex>(int)Math.Ceiling((int)ViewState["rowscount"]/pagesize))

{

PageIndex = (int)Math.Ceiling((int)ViewState["rowscount"]/pagesize);

}

else

{

PageIndex--;

}

}

//简单起见,将所有的linkbutton全部改为enable=true

FirstPage.Enabled = true;

NextPage.Enabled = true;

LastPage.Enabled = true;

PrevPage.Enabled = true;

ViewState["currentpage"] = PageIndex;

FillGrid(proc,(int)ViewState["currentpage"],pagesize,datagrid);

}

catch(Exception)

{

return;

}

}

private void NewPageGo_Click(object sender, System.EventArgs e)

{

NewPage_Go(NewPageIndex.Text.Trim());

}

}

}

PageForm.aspx

==================================================================

<%@ Page language="c#" Codebehind="PageForm.aspx.cs" AutoEventWireup="false" Inherits="Ex_Test.PageForm" %>

<%@ Register TagPrefix="MK" TagName="PageChange" src="PageChange.ascx"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

<HEAD>

<title>PageForm</title>

<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">

<meta name="CODE_LANGUAGE" Content="C#">

<meta name="vs_defaultClientScript" content="JavaScript">

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

</HEAD>

<body MS_POSITIONING="GridLayout">

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

<asp

ataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 57px; POSITION: absolute; TOP: 54px" runat="server" AllowCustomPaging="True" AllowPaging="True">

<PagerStyle Visible="False"></PagerStyle>

</asp

ataGrid>

<asp

anel id="Panel1" style="Z-INDEX: 102; LEFT: 60px; POSITION: absolute; TOP: 20px" runat="server" Width="634px">

<MK

ageChange id="pc" runat="server"></MK
ageChange>

</asp

anel><FONT face="宋体"></FONT>

</form>

</body>

</HTML>

=================================================================

PageForm.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;

namespace Ex_Test

{

/// <summary>

/// PageForm 的摘要说明。

/// </summary>

public class PageForm : System.Web.UI.Page

{

protected System.Web.UI.WebControls.DataGrid DataGrid1;

protected System.Web.UI.WebControls.Panel Panel1;

protected Ex_Test.PageChange pc;

private void Page_Load(object sender, System.EventArgs e)

{

// 在此处放置用户代码以初始化页面

//if(!IsPostBack)

//{

pc._CurrentPage = 0;

pc._datagrid = DataGrid1;

pc._pageSize =7;

pc._proc = "Page_Change";

//}

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

}

}

=================================================================

存储过程:

Create PROCEDURE dbo.Page_Change

/*

(

@parameter1 datatype = default value,

@parameter2 datatype OUTPUT

)

*/

(

@PageSize int,

@CurrentPage int

)

AS

/* SET NOCOUNT ON */

select *,IDENTITY(int,1,1) as Num into #TempAuthors from Authors

select * from #TempAuthors where Num > (@PageSize*@CurrentPage) and Num < (@PageSize*@CurrentPage+@PageSize+1)

RETURN

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有