ITPub博客

首页 > 应用开发 > IT综合 > 个性化的分页实现 (转)

个性化的分页实现 (转)

原创 IT综合 作者:amyz 时间:2007-08-15 16:45:21 0 删除 编辑
个性化的分页实现 (转)[@more@]

我们前面讲的分页,只不过是通过修改DataGrid的属性来实现分页,这样有这样的好处,最大的就是简单,呵呵,根本不用操心,分页是如何产生的。

但是它同样有缺点,不能按照我们想像的产生各种我们需要的样式。

没有办法,想个性化功能,只有自已动手来做了,呵呵。

我们一步步的来,首先是导入需要的命名空间。下面的例子,其实也是我从国外找来的,再加上点个人的东东,再汉化,呵呵。今天心情很好,我连标签色彩都给大家显示出来了。呵呵,更利于大家看程序。

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.sql" %>

我们先看看,我们的web控件是哪些,再看看代码是怎么写的,这样比较好:)

个性化的分页实例^&^

RM runat=server>

<ASP:DataGrid id="MyDataGrid" runat="server"
AllowPaging="True"
PageSize="10"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
OnPageIndExchanged="MyDataGrid_Page"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
AutoGenerateColumns="false"

>





Text="首页"
CommandArgument="0"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>
 
Text="前页"
CommandArgument="prev"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>
 
Text="后页"
CommandArgument="next"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>
 

Text="末页"
CommandArgument="last"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>


Text="显示内置的页数"
Font-Name="Verdana"
Font-Size="8pt"
AutoPostBack="true"
/>






从上面的例子我们可以看出点击LinkButton控件OnClick触发的是PageButtonClick事件,DataGrid页面OnPageIndexChanged改变触发的是MyDataGrid_Page事件,我们以后就是就是要编写这两件事件的代码

下面是先要查询的数据库的信息,用一个函数表示,因为经常用到:),我打开的表,呵呵,是我们工作室管理区的登陆记录表(哈又卖了点我们工作室的秘密给大家)

ICollection CreateDatasource()
{
  /*
  读取数据库的信息,获得DataView
  */
  SQLConnection MyConnection = new SQLConnection("server=localhost;uid=sa;pwd=123456;database=aspcn");
  SQLDataSetCommand MyDataSetCommand = new SQLDataSetCommand("select * from admin_enter order by Enter_Time desc",MyConnection);
  DataSet ds= new DataSet();
  MyDataSetCommand.FillDataSet(ds,"admin_enter");
  return ds.Tables["admin_enter"].DefaultView;
}

然后中是Page_Load函数,在这里主要是判断一下是否显示DataGrid自带的那些分页数字,使用的是PageStyle的Visible属性:

void Page_Load(object sender, EventArgs e)
{
  断是否隐藏PagerStyle-Mode
  if (chk1.Checked)
  {
  MyDataGrid.PagerStyle.Visible=true;
  }
  else
  {
  MyDataGrid.PagerStyle.Visible=false;
  }

  BindGrid();
}

下面是处理点击事件的PagerButtonClick,这是我们的核心部分,其实我们操作的也只是DataGrid的CurrentPageIndex属性。如果CurrentPageIndex小于PageCount则有下一页,如果CurrentPageIndex大于0则表示有前一页。

void PagerButtonClick(Object sender, EventArgs e)
{
  得LinkButton的参数值
  String arg = ((LinkButton)sender).CommandArgument;

  switch(arg)
  {
  case ("next"):
  if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
  MyDataGrid.CurrentPageIndex ++;
  break;
  case ("prev"):
  if (MyDataGrid.CurrentPageIndex > 0)
  MyDataGrid.CurrentPageIndex --;
  break;
  case ("last"):
  MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
  break;
  default:
  页值
  MyDataGrid.CurrentPageIndex = arg.ToInt32();
  break;
  }
  BindGrid();
}

下面是MyDataGrid_Page,主要操作是调用BindGrid函数,以将数据交给DataGrid显示:

void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e)
{
  理按下数字的方法
  BindGrid();
}

最后是两个函数,他们的作用,我都注释了:)

 

void BindGrid()
{
  DataView绑定到DataGrid上去
  MyDataGrid.DataSource = CreateDataSource();
  MyDataGrid.DataBind();
  ShowStats();
}

 

void ShowStats()
{
  示页面信息
  lblCurrentIndex.Text = "当前页数为: " + ((int)MyDataGrid.CurrentPageIndex+1);
  lblPageCount.Text = "总页数是: " + MyDataGrid.PageCount;
}

 

到此为止,我们的个性化页面已经完成了(全部代码和显示看一下节),我们总的思想就是使用LinkButton控件做为翻页的标帜,通过判断LinkButton的CommandArgument值,操作DataGrid的CurrentPageIndex属性,以达到翻页的效果。


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10752019/viewspace-958752/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3982
  • 访问量
    7896280