ASP.NET自定义分页控件
这次开发的CRM中有许多地方都使用到了ASP.NET中的GridView控件,这个控件的功能其实都是很全的,但是此次开发中不允许使用控件自带的分页,而是要求开发人员自己写。由于自己写的分页代码比较多,且项目中使用的GridView控件也是很多,为了偷懒+减少代码量…于是昨天晚上花了点时间做了这么个自定义WEB用户控件,写的时候也遇到了些问题,不过随着逐步的分析都逐一解决了,以下是该控件的源码。
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Collections.Generic;
- /**
- * 功能:对GridView数据分页
- * 作者:Neeke
- * 版权所有:尼克技术博客 http://www.ineeke.com
- * */
- public partial class WebUserControl : System.Web.UI.UserControl
- {
- PagedDataSource pds = new PagedDataSource();
- //待分页的数据源
- public IEnumerable DataSource;
- //每页显示数据条数
- public int pageSize = 10;
- //待分页显示的GridView对象
- public GridView PageGridView;
- //当前页
- public int CurrentPage
- {
- get { return (Int32)ViewState["CurrentPage"]; }
- set { ViewState["CurrentPage"] = value; }
- }
- //分页个数
- public int Count
- {
- get { return (Int32)ViewState["Count"]; }
- set { ViewState["Count"] = value; }
- }
- //数据绑定
- public void PageDataBind()
- {
- if (!IsPostBack)
- {
- CurrentPage = 0;
- Count = 0;
- }
- //用于判断是否越界
- if (CurrentPage > Count || CurrentPage < 0)
- {
- return;
- }
- try
- {
- pds.DataSource = DataSource;
- pds.AllowPaging = true;
- pds.PageSize = pageSize;
- pds.CurrentPageIndex = CurrentPage;
- Count = pds.PageCount;
- this.lblTotalCount.Text = pds.DataSourceCount.ToString();
- this.lblPageSize.Text = pageSize.ToString();
- this.lblCurrentPage.Text&nbs
p;= Convert.ToString(CurrentPage + 1); - this.lblTotalPage.Text = Convert.ToString(Count);
- PageGridView.DataSource = pds;
- PageGridView.DataBind();
- }
- catch (Exception ex)
- {
- Response.Write("<script>alert(‘没有您输入的页!’);</script>");
- }
- }
- //第一页按钮按下时触发
- protected void LinkFirst_Click(object sender, EventArgs e)
- {
- CurrentPage = 0;
- PageDataBind();
- }
- //前一页按钮按下时触发
- protected void LinkPrie_Click(object sender, EventArgs e)
- {
- if (CurrentPage <= 0)
- {
- CurrentPage = 0;
- }
- else
- {
- CurrentPage = CurrentPage-1;
- }
- PageDataBind();
- }
- //下一页按钮按下时触发
- protected void LinkNext_Click(object sender, EventArgs e)
- {
- if (CurrentPage >= (Count - 1))
- {
- CurrentPage = Count - 1;
- }
- else
- {
- CurrentPage=CurrentPage+1;
- }
- PageDataBind();
- }
- //最后一页按钮按下时触发
- protected void LinkLast_Click(object sender, EventArgs e)
- {
- CurrentPage = Count - 1;
- PageDataBind();
- }
- //GO按钮按下时触发
- protected void btnGo_Click(object sender, EventArgs e)
- {
- CurrentPage = int.Parse(this.txtGoPage.Text)-1;
- PageDataBind();
- }
- }
使用方法:
1.将此控件拖入到网页中。
2.在该网页的Page_Load()事件中写入如下代码。
- protected void Page_Load(object sender, EventArgs e)
- {
- this.WebUserControl1.DataSource = SaleChanceManager.GetChanceAll();//设置待数据源
- this.WebUserControl1.pageSize = 5;//设置每页数据条数
- this.WebUserControl1.PageGridView = this.GridView1;//设置待分页的GridView
- this.WebUserControl1.PageDataBind();//调用绑定方法
- }
除非另有声明,本站遵循【署名-非商业性使用-相同方式共享 3.0 共享协议】授权。
转载原创文章请注明,转载自:Neeke[http://www.ineeke.com]
本文链接: http://www.ineeke.com/archives/AspDotNetZiDingYiFenYeKongJian/

O(∩_∩)O哈哈~ 任何事物都有两面性嘛。
其实可以写很多的自定义控件,以便以后用着方便。
比如我自己就经常写写DLL文件,做ASP.NET项目时直接引用进来,然后直接就点出来了,操作数据库自动MD5加密截取字符串,还能固定截取长度,位置。什么的,废点劲写个比较全面的DLL文件,以后用的闲荡方便,但是我现在自己写的程序就算给了别人,他们也看不懂:(主要自己定义的DLL太多了。。。。
语法真像java,不仔细看都以为是java的“儿子”。
解析的很详细哦!呵呵,如果是ASP的就好了!刚开始学asp不久!
帅~O(∩_∩)O~