1、 1GridView 分页系列(精装版)1: GridView 自带分页:GridView 自带的分页,是假分页,他每次从数据库把数据全部查询出之后,通过分页的算法,进行按每页数量进行分页。分页的属性元素:分页功能的实现就是通过对这些属性元素的操作实现的。/this.GvShow.PageIndex 当前页的索引/this.GvShow.PageCount 总共的页数/this.GvShow.Rows.Count 当前页签内的gridview的行数/this.GvShow.PageSize 每页的记录数/this.GvShow.PageIndex*this.GvShow.rows.count
2、+ 1 行索引设置普通的 GridView 分页:属性 AllowPaging=“True“、PageSize=“2“ 设置分页事件onpageindexchanging=“GvShow_PageIndexChanging“后台方法绑定:protected void GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)this.GvShow.PageIndex = e.NewPageIndex;BindView();2:自定义样式的 GridView 自带分页:普通的 GridView 自带的分页,不带样式,只是普通的
3、 1,2,3 等,如果希望获取到具有其他分页样式就应该设置属性后台访问此属性的实例:this.GvShow.PagerSettings.FirstPageText = “首页“;this.GvShow.PagerSettings.LastPageText = “尾页“;this.GvShow.PagerSettings.NextPageText = “下一页“;this.GvShow.PagerSettings.PreviousPageText = “上一页“;this.GvShow.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast
4、;通过属性可以设置GRIDVIEW 分页的样式3:在分页模板中自定义分页的样式,虽然微软开辟了这个模板提供给用户类似于自定义分页的功能,但这个功能完全还是基于微软的 GridView 自带的分页进行的,属性的 Visable 的属性必须是 true AllowPaging=“true“ 与 PageSize=“3“属性页都要进行相关的有效设置才可以。这种情况下的分页可以不使用onpageindexchanging=“GvShow_PageIndexChanging“微软自带的分页事件,开发自己独立的分页事件与方法即可范例:前台代码:-% 3首页 上一页 下一页 尾页页次:0/0页 共0条记录0
5、条记录/页后台代码:namespace GridViewDemo.GridView分页系列public partial class GridView自定义分页02 : System.Web.UI.Pageprotected void Page_Load(object sender, EventArgs e)if (!IsPostBack)BindView();InitButtons();4private void BindView()DataTable dt = UserDemoAccess.GetUserSouce();ViewState“RowCounts“ = dt.Rows.Count
6、.ToString();this.GvShow.DataSource = dt;this.GvShow.DataBind();protected void GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)this.GvShow.PageIndex = e.NewPageIndex;BindView();InitButtons();protected void GvShow_RowDataBound(object sender, GridViewRowEventArgs e)if (e.Row.RowType = D
7、ataControlRowType.DataRow)e.Row.Attributes.Add(“onmouseover“, “this.setAttribute(BKC,this.style.backgroundColor);this.style.cursor=default,this.style.backgroundColor=#ffff99“);e.Row.Attributes.Add(“onmouseout“, “this.style.backgroundColor=this.getAttribute(BKC);“);/if(e.Row.RowType = DataControlRowT
8、ype.Pager)/ GridViewRow gr = e.Row;/ /页次 - 第几页/ Label txtNowPage = gr.FindControl(“txtNowPage“) as Label;/ /总页数/ Label txtAllPage = gr.FindControl(“txtAllPage“) as Label;/ /总记录数/ Label txtTotal = gr.FindControl(“txtTotal“) as Label;/ /条记录/页/ Label txtNowRed = gr.FindControl(“txtNowRed“) as Label;/ t
9、xtNowPage.Text = (this.GvShow.PageIndex+1).ToString();/ txtAllPage.Text = this.GvShow.PageCount.ToString();/ txtTotal.Text = ViewState“RowCounts“.ToString();/ txtNowRed.Text = this.GvShow.PageSize.ToString();5/protected void cmdCheck_CheckedChanged(object sender, EventArgs e)/CheckBox cmdCheck = thi
10、s.GvShow.BottomPagerRow.FindControl(“cmdCheck“) as CheckBox;/if (cmdCheck.Checked)/ this.GvShow.PagerSettings.Mode = PagerButtons.Numeric;/protected void GvShow_RowCommand(object sender, GridViewCommandEventArgs e)/控制页签switch(e.CommandName)case “begin“:this.GvShow.PageIndex = 0; break;case “before“:
11、if(this.GvShow.PageIndex 0)this.GvShow.PageIndex -= 1; break;case “after“: if(this.GvShow.PageIndex 1)cmdbegin.Enabled = true;cmdbefore.Enabled = true;cmdafter.Enabled = true;cmdend.Enabled = true;if(this.GvShow.PageIndex = 0)7cmdbegin.Enabled = false;cmdbefore.Enabled = false;else if(this.GvShow.Pa
12、geIndex = this.GvShow.PageCount-1)cmdafter.Enabled = false;cmdend.Enabled = false;此外还可以不利用GridView自带的分页模板标记可以在GridView的外部构造一个talbe与gridview对应,这样更加灵活,也应用于div中的GridView。前台代码:首页 上一页 下一页 尾页页次:0/0页 共0条记录0条记录/页8这俩种方式的区别在于:适用内部的分页模板标记,在触发事件后,不用重新绑定 GridView,这个是由GridView 内部的分页机制自动完成的,而采用外部构造的方式,在执行完之后,必须重新绑
13、定gridview,因为这个时候的 pageIndex 等信息已经变化,需要重新进行绑定。后台代码:protected void PagerButton_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e) switch (e.CommandName)case “begin“:this.GvShow.PageIndex = 0; break;case “before“:if (this.GvShow.PageIndex 0)this.GvShow.PageIndex -= 1; break;case “afte
14、r“:if (this.GvShow.PageIndex 10首页 上一页 下一页 尾页页次:0/0页 共0条记录0条记录/页后台页面:namespace GridViewDemo.GridView分页系列11public partial class GridView自定义分页04 : System.Web.UI.Pageprotected void Page_Load(object sender, EventArgs e)if (!IsPostBack)BindView();InitButtons();InitDrp();private void BindView()DataTable dt
15、 = UserDemoAccess.GetUserSouce();ViewState“PageCount“ = dt.Rows.Count.ToString();this.GvShow.DataSource = dt; ;this.GvShow.DataBind();protected void GvShow_RowDataBound(object sender, GridViewRowEventArgs e)if (e.Row.RowType = DataControlRowType.DataRow)e.Row.Attributes.Add(“onmouseover“, “this.setA
16、ttribute(BKC,this.style.backgroundColor);this.style.cursor=default,this.style.backgroundColor=#ffff99“);e.Row.Attributes.Add(“onmouseout“, “this.style.backgroundColor=this.getAttribute(BKC);“);protected void PagerButton_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)switch (e.Co
17、mmandName)case “begin“:this.GvShow.PageIndex = 0; break;case “before“:if (this.GvShow.PageIndex 0)12this.GvShow.PageIndex -= 1; break;case “after“:if (this.GvShow.PageIndex 1)this.cmdbegin.Enabled = true;this.cmdbefore.Enabled = true;this.cmdafter.Enabled = true;this.cmdend.Enabled = true;if (this.G
18、vShow.PageIndex = 0)this.cmdbegin.Enabled = false;this.cmdbefore.Enabled = false;else if (this.GvShow.PageIndex = this.GvShow.PageCount - 1)this.cmdafter.Enabled = false;13this.cmdend.Enabled = false;protected void lbtnTurnNewPage_Click(object sender, EventArgs e)tryint pageIndexNumber = Convert.ToI
19、nt32(this.txtTurn.Text.Trim();if (pageIndexNumber this.GvShow.PageCount)Response.Write(“越界!“);return;else if (pageIndexNumber 模板实现的假分页实例:(可以在绑定的时候加入 dropdownlist 的页面选择)前台页面:15 第“页共“页“Text=“首页“Text=“上一页“Text=“下一页“Text=“尾页“Width=“20px“namespace GridViewDemo.GridView分页系列public partial class GridView自定义
20、分页05 : System.Web.UI.Pageprotected void Page_Load(object sender, EventArgs e)if (!IsPostBack)BindView();private void BindView()DataTable dt = UserDemoAccess.GetUserSouce();this.GvShow.DataSource = dt; ;this.GvShow.DataBind();protected void GvShow_RowDataBound(object sender, GridViewRowEventArgs e)17
21、if (e.Row.RowType = DataControlRowType.DataRow)e.Row.Attributes.Add(“onmouseover“, “this.setAttribute(BKC,this.style.backgroundColor);this.style.cursor=default,this.style.backgroundColor=#ffff99“);e.Row.Attributes.Add(“onmouseout“, “this.style.backgroundColor=this.getAttribute(BKC);“);protected void
22、 GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)GridViewRow gr = this.GvShow.BottomPagerRow;TextBox txtNewPageIndex = gr.FindControl(“txtNewPageIndex“) as TextBox;string content = CheckPageIndex();if (content != string.Empty)Show(this, content);/如果发现异常,则重新将文本框中的值,置换成当前页签的!txtNewPage
23、Index.Text = (this.GvShow.PageIndex+1).ToString();return;/NewPageIndex 本页跳转点击go的时候,为-2,增加一个处理!微软自带分页中,选中页签当前页无法再点击/此时的e.NewPageIndex默认为-2else if (e.NewPageIndex this.GvShow.PageCount)cotent = “你越位了,朋友!“;catch cotent = “请输入数字!“;return cotent;19public static void Show(System.Web.UI.Page page, string msg)page.ClientScript.RegisterStartupScript(page.GetType(), “message“, “alert(“ + msg.ToString() + “);“);