GridView控件自定义分页详解

  • 格式:doc
  • 大小:58.00 KB
  • 文档页数:9

下载文档原格式

  / 12
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

GridView控件自定义分页详解

在这里我们将用一个隐藏字段来保存这个PageIndex,即当前页码.当点击上一页时,将它的值减一,知道为0,要注意的一点这里的第一页页码是0而不是1.下面看看代码,然后我们再分析分析!

1

2

3

4

5 DataTextField="Title" HeaderText="新闻标题" ItemStyle-Width="70%"/>

6

7

8 ShowEditButton="True"/>

9

10

11

12

13 首页

14 上一页

15 下一页

16 尾页

17

CS文件中的代码:

1 protected void PagerButton_Click(object sender, EventArgs e)

2 {

3 int pageIndx = Convert.ToInt32(CurrentPage.Value);

4 int totals = NewsManager.GetNews(0, pageSize).TotalRecords;

5 int pages = (totals % pageSize) == 0 ? (totals / pageSize) : (totals / pageSize + 1);

6 string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

7 switch (arg)

8 {

9 case "prev":

10 if (pageIndx > 0)

11 {

12 pageIndx -= 1;

13 }

14 break;

15 case "next":

16 if (pageIndx < pages - 1)

17 {

18 pageIndx += 1;

19 }

20 break;

21 case "last":

22 pageIndx = pages - 1;

23 break;

24 default:

25 pageIndx = 0;

26 break;

27 }

28 CurrentPage.Value = pageIndx.ToString();

29 NewsGrid.DataSource = NewsManager.GetNews(pageIndx , pageSize).Entities;

30 NewsGrid.DataBind();

31 }

如何在GridView中增加效果

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {

//将满足特定条件的行标为高亮

if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行{

int money = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MONEY"));//取当前行的列值

if (money == 77)

e.Row.BackColor = Color.Red;

//string customer = (string)DataBinder.Eval(e.Row.DataItem, "CUSTOMER");

string customer = DataBinder.Eval(e.Row.DataItem, "CUSTOMER").ToString();

if (customer == "sdf")

e.Row.BackColor = Color.Red;

}

//加入鼠标滑过的高亮效果

if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行{

//当鼠标放上去的时候先保存当前行的背景颜色并给附一颜色

e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='yellow',this.style.fontWeight='';");

//当鼠标离开的时候将背景颜色还原的以前的颜色

e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

}

//单击行改变行背景颜色

if (e.Row.RowType == DataControlRowType.DataRow)

{