• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

一个DataGrid分页控件,c#写的,再vb.net中调用,将会不断完善的

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace wxc
{
 /// <summary>
 ///作者:kasafuma
 /// </summary>
 [DefaultProperty("Text"),
  ToolboxData("<{0}:Pager runat=server></{0}:Pager>")]
 public class Pager : System.Web.UI.Control,INamingContainer
 {
  [Description("选择页事件")]
  public event EventHandler PageChanged;
  #region 控件属性列表
  [Browsable(false),
  Description("数据集所有页的记录总数"),
  Category("分页"),
  DefaultValue(0)]
  public int RecordCount
  {
   get
   {
    object obj=ViewState["RecordCount"];
    return (obj==null)?0:(int)obj;
   }
   set
   {
    ViewState["RecordCount"]=value;
   }
  }

  [Browsable(true),
  Description("每页显示的记录数"),
  Category("分页"),
  DefaultValue(10)]
  public int PageSize
  {
   get
   {
    object obj=ViewState["PageSize"];
    return (obj==null)?10:(int)obj;
   }
   set
   {
    ViewState["PageSize"]=value;
   }
  }
  
  [Browsable(false),
  Description("当前显示页"),
  Category("分页"),
  DefaultValue(0)]
  public int CurrentPage
  {
   get
   {
    object obj=ViewState["CurrentPage"];
    return (obj==null)?0:(int)obj;
   }
   set
   {
    ViewState["CurrentPage"]=value;
   }
  }

  
  [Browsable(false),
  Description("数据集页总数"),
  Category("分页"),
  DefaultValue(0)]
  public int PageCount
  {
   get
   {
    object obj=ViewState["PageCount"];
    return (obj==null)?0:(int)obj;
   }
   set
   {
    ViewState["PageCount"]=value;
   }
  }
  [Browsable(true),
  Description("首页按钮文本,例如可用:第一页"),
  Category("分页")]
  public string FirstPageText
  {
   get
   {
    object obj=ViewState["FirstPageText"];
    return (obj==null)?null:(string)obj;
   }
   set
   {
    ViewState["FirstPageText"]=value;
   }
  }
  [Browsable(true),
  Description("前页按钮文本,例如可用:上一页"),
  Category("分页")]
  public string PrevPageText
  {
   get
   {
    object obj=ViewState["PrevPageText"];
    return (obj==null)?null:(string)obj;
   }
   set
   {
    ViewState["PrevPageText"]=value;
   }
  }

  [Browsable(true),
  Description("下页按钮文本,例如可用:下一页"),
  Category("分页")]
  public string NextPageText
  {
   get
   {
    object obj=ViewState["NextPageText"];
    return (obj==null)?null:(string)obj;
   }
   set
   {
    ViewState["NextPageText"]=value;
   }
  }
  [Browsable(true),
  Description("尾页按钮文本,例如可用:最后页"),
  Category("分页")]
  public string LastPageText
  {
   get
   {
    object obj=ViewState["LastPageText"];
    return (obj==null)?null:(string)obj;
   }
   set
   {
    ViewState["LastPageText"]=value;
   }
  }
  #endregion
  protected override void CreateChildControls()
  {
   
         Label lbl_PageInfo = new Label(); //0
   lbl_PageInfo.Font.Size=10;
   lbl_PageInfo.Text = "尚未绑定";
   Controls.Add(lbl_PageInfo);
                 
   LinkButton btn_First = new LinkButton();   //1
   btn_First.Text = FirstPageText.ToString();
   btn_First.Font.Size=10;
   Controls.Add(btn_First);
   btn_First.Visible = false;
   btn_First.Click += new System.EventHandler(btnFirst_Click);

   Controls.Add(new LiteralControl(" "));    //2

   LinkButton btn_Prev = new LinkButton();    //3
   btn_Prev.Text =PrevPageText.ToString();
   btn_Prev.Font.Size=10;
   Controls.Add(btn_Prev);
   btn_Prev.Visible = false;
   btn_Prev.Click += new System.EventHandler(btnPrev_Click);

   Controls.Add(new LiteralControl(" "));    //4

   LinkButton btn_Next = new LinkButton();    //5
   btn_Next.Text = ViewState["NextPageText"].ToString();
   btn_Next.Font.Size=10;
   Controls.Add(btn_Next);
   btn_Next.Visible = false;
   btn_Next.Click += new System.EventHandler(btnNext_Click);

   Controls.Add(new LiteralControl(" "));    //6

   LinkButton btn_Last = new LinkButton();    //7
   btn_Last.Text = ViewState["LastPageText"].ToString();
   btn_Last.Font.Size=10;
   Controls.Add(btn_Last);
   btn_Last.Visible = false;
   btn_Last.Click += new System.EventHandler(btnLast_Click);
  
   Controls.Add(new LiteralControl("&nbsp;&nbsp;")); //8

   Label lbl_1 = new Label();       //9
   lbl_1.Text = "转到";
   lbl_1.Font.Size=10;
   lbl_1.Visible = false;
   Controls.Add(lbl_1);

   DropDownList drop_CurPage = new DropDownList();  //10
   drop_CurPage.Items.Add("0");
   drop_CurPage.Font.Size=10;
   Controls.Add(drop_CurPage);
   drop_CurPage.AutoPostBack = true;
   drop_CurPage.Visible = false;
   drop_CurPage.SelectedIndexChanged += new System.EventHandler(ddlCurrentPage_SelectedIndexChanged);

   Label lbl_2 = new Label();       //11
   lbl_2.Text = "页";
   lbl_2.Font.Size=10;
   lbl_2.Visible = false;
   Controls.Add(lbl_2);
  }
  protected override void OnPreRender(EventArgs e)
  {
   if (!Page.IsPostBack)
   {
    BindPager();
   }
  }
  protected virtual void PageClick(object sender, EventArgs e)
  {
   if (PageChanged != null)
   {
    PageChanged(this,e);
   }
  }


  #region 页面按钮和选择框事件
  private void ddlCurrentPage_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   CurrentPage = ((DropDownList)Controls[10]).SelectedIndex;
   BindPager();
             SendPageChanged();
  }

  private void btnFirst_Click(object sender, System.EventArgs e)
  {
   CurrentPage = 0;
   BindPager();
            SendPageChanged();
  }

  private void btnPrev_Click(object sender, System.EventArgs e)
  {
   CurrentPage = CurrentPage-1;
   BindPager();
           SendPageChanged();
  }

  private void btnNext_Click(object sender, System.EventArgs e)
  {
   CurrentPage = CurrentPage+1;
   BindPager();
            SendPageChanged();
  }

  private void btnLast_Click(object sender, System.EventArgs e)
  {
   CurrentPage = CurrentPage-1;
   BindPager();
   SendPageChanged();

  }
  private void SendPageChanged()
  {
   if (PageChanged != null)
    PageClick(this, EventArgs.Empty);
  
  }
  #endregion
        #region 绑定页面
  public void BindPager()
  {
    PageCount=(RecordCount%PageSize>0) ? RecordCount/PageSize+1:RecordCount/PageSize;        //总页数
   ((Label)Controls[0]).Text="共<font color=red>"+RecordCount.ToString()+"</font>条信息<font color=red>"+PageSize.ToString()+"</font>条/页 当前<font color=red>"+(CurrentPage+1).ToString()+"</font>/<font color=red>"+PageCount.ToString()+"</font>页&nbsp;&nbsp;"; 
   ((DropDownList)Controls[10]).Items.Clear();
            for(int i=0;i<PageCount;i++)
   {
    ((DropDownList)Controls[10]).Items.Add(Convert.ToString(i+1));
   }
   ((DropDownList)Controls[10]).SelectedIndex = ((DropDownList)Controls[10]).Items.IndexOf(((DropDownList)Controls[10]).Items.FindByValue(Convert.ToString(CurrentPage+1)));


  
   ((LinkButton)Controls[1]).Visible = true;
   ((LinkButton)Controls[3]).Visible   = true;
   ((LinkButton)Controls[5]).Visible  = true;
   ((LinkButton)Controls[7]).Visible  = true;
   ((Label)Controls[9]).Visible   = true;
   ((Label)Controls[11]).Visible   = true;
   ((DropDownList)Controls[10]).Visible = true;

   ((LinkButton)Controls[1]).Enabled = true;
   ((LinkButton)Controls[3]).Enabled   = true;
   ((LinkButton)Controls[5]).Enabled  = true;
   ((LinkButton)Controls[7]).Enabled  = true;

   if (CurrentPage==0)
   {
    ((LinkButton)Controls[1]).Enabled = false;
    ((LinkButton)Controls[3]).Enabled   = false;
    ((LinkButton)Controls[5]).Enabled  = true;
    ((LinkButton)Controls[7]).Enabled  = true;
   }
   if (CurrentPage==PageCount-1)
   {
    ((LinkButton)Controls[1]).Enabled = true;  //如果是自定义图片按钮可以 1ibtnFirst.Attributes["disabled"] = "disabled" 不能点; 2ibtnFirst.Attributes.Remove("disabled") ;能点
    ((LinkButton)Controls[3]).Enabled   = true;
    ((LinkButton)Controls[5]).Enabled  = false;
    ((LinkButton)Controls[7]).Enabled  = false;
   }
   if ((PageCount-1) == 0 )
   {
    ((LinkButton)Controls[1]).Enabled = false;
    ((LinkButton)Controls[3]).Enabled   = false;
    ((LinkButton)Controls[5]).Enabled  = false;
    ((LinkButton)Controls[7]).Enabled  = false;
   }
  }
  #endregion

 }
}
调用时候
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码
        If Not IsPostBack Then   
                       BindData(0)
        End If
    End Sub
 Private Sub BindData(ByVal CurrentPage As Integer)
        txtSQL = "select * from Orders"
        DBSet = ExecutePager(txtSQL, CurrentPage, Pager1, ErrorMsg)
        Datagrid2.DataSource = DBSet.Tables(0).DefaultView
        Datagrid2.DataBind()    
        Pager1.RecordCount = 47 //当然这样写肯定是错的,这里还需要计算一下页数
        Pager1.DataBind()
    End Sub

Private Sub Change(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pager1.PageChanged
        BindData(Pager1.CurrentPage)
    End Sub


在模块中写个公用函数:
Public Function ExecutePager(ByVal strSQL As String, ByVal CurrentPage As Integer, ByVal Pager As Object, ByRef errMsg As String) As DataSet
        Dim cnn As SqlClient.SqlConnection
        Dim cmd As New SqlClient.SqlCommand
        Dim adpt As SqlClient.SqlDataAdapter
        Dim rst As New DataSet
        Dim SplitSQL() As String
        errMsg = ""
        Try
            cnn = New SqlClient.SqlConnection("data source=(local);initial catalog=Northwind;user id=sa;pwd='hyaocuk!'")
            adpt = New SqlClient.SqlDataAdapter(strSQL, cnn)
            adpt.Fill(rst, Pager.PageSize * CurrentPage, Pager.PageSize, "tb")
'这种分页效率也不是很高的,最好调用存储过程分页
            ExecutePager = rst
        Catch ex As Exception
            errMsg = ex.Message
        Finally
            rst = Nothing
            cnn = Nothing
        End Try
    End Function


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap