[原创]自定义分页控件(dll服务器控件)代码学习与探讨!

类别:Asp 点击:0 评论:0 推荐:

本控件提供的相关内容:
 三个文件: 1.Pager.cs      2.WebForm.aspx    3 WebForm.aspx.cs
其中1为翻页控件.  2及3是用作测试的文档及源文件.
试用需要做的事:  1.根据提示转成相应的文件.及建立相关项目编译
                               2.根据数据库连接信息建立相应的数据库(TestDataBase)及数据表(News)
                               3.建立相对应的站点. 

--------Pager.cs  服务器控件源文件----------------------------------------------------------------------------
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace C_Pager
{
 public class Pager : Control, INamingContainer
 {
  [Description("操作员选择不同页时产生的页更新事件,通过此事件处理把旧页面显示信息更新到选择页对应的数据信息")]
  public event EventHandler PageChanged;

  private int rectotal = 0;
  private int pagesize = 10;
  private int curpage  = 0;
  private int lang  = 0 ;
  private int pagetotal = 0;

  private string firstpagetext ="<FONT face=webdings>9</FONT>";
  private string prevpagetext = "<FONT face=webdings>3</FONT>";
  private string nextpagetext = "<FONT face=webdings>4</FONT>";
  private string lastpagetext = "<FONT face=webdings>:</FONT>";

  protected virtual void temp_Click(object sender, EventArgs e)
  {
   if (PageChanged != null)
   {
    PageChanged(this,e);
   }
  }

  #region Pager控件使用帮助及参数说明
  /*
   本控件带有三个自定义属性和一个方法.具体如下:
   属性:
    RecTotal 记录总数 默认值:0
    PageSize 每页记录数 默认值:0
    CurPage  当前页  默认值:0
    Lang  版本语言 默认值:0 注:0为中文版,1为英文版
   方法:
    PagerBind 绑定当前自定义控件,如不绑定,该控件则不能显示到页面上
   */
  #endregion

  #region 控件属性列表

  [Category("分页控制"),Description("数据集所有页记录总数")]
  public int RecTotal 
  {
   get { return rectotal; }
   set { rectotal = value;  }
  }

  [Category("分页控制"),Description("每页显示记录数量")]
  public int PageSize 
  {
   get { return pagesize; }
   set { pagesize = value;  }
  }

  [Category("分页控制"),Description("当前显示页")]
  public int CurPage 
  {
   get { return curpage; }
   set { curpage = value;  }
  }

  [Category("分页控制"),Description("数据集页总数")]
  public int PageTotal 
  {
   set { pagetotal = value;  }
  }

  [Category("分页控制"),Description("翻页条显示语言别,0为中文,1为英文")]
  public int Lang  
  {
   get { return lang;  }
   set { lang = value;   }
  }

  [Category("分页控制"),Description("首页按钮文本,例如可用:第一页")]
  public string FirstPageText  //
  {
   get { return firstpagetext;  }
   set { firstpagetext = value;   }
  }

  [Category("分页控制"),Description("前页按钮文本,例如可用:上一页")]
  public string PrevPageText  
  {
   get { return prevpagetext;  }
   set { prevpagetext = value;   }
  }

  [Category("分页控制"),Description("下页按钮文本,例如可用:下一页")]
  public string NextPageText  
  {
   get { return nextpagetext;  }
   set { nextpagetext = value;   }
  }

  [Category("分页控制"),Description("尾页按钮文本,例如可用:最后页")]
  public string LastPageText  
  {
   get { return lastpagetext;  }
   set { lastpagetext = value;   }
  }

  #endregion

  protected override void CreateChildControls()
  {
   ViewState["FirstPageText"]= firstpagetext;
   ViewState["PrevPageText"] = prevpagetext;
   ViewState["NextPageText"] = nextpagetext;
   ViewState["LastPageText"] = lastpagetext;
    
   Label lbl_PageInfo = new Label();     //0
   lbl_PageInfo.Text = "尚未绑定";
   Controls.Add(lbl_PageInfo);
                 
   LinkButton btn_First = new LinkButton();   //1
   btn_First.Text = ViewState["FirstPageText"].ToString();
   Controls.Add(btn_First);
   btn_First.Visible = false;
   btn_First.Click += new System.EventHandler(btn_First_Click);

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

   LinkButton btn_Pre = new LinkButton();    //3
   btn_Pre.Text = ViewState["PrevPageText"].ToString();
   Controls.Add(btn_Pre);
   btn_Pre.Visible = false;
   btn_Pre.Click += new System.EventHandler(btn_Pre_Click);

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

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

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

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

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

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

   Label lbl_2 = new Label();       //11
   lbl_2.Text = "页";
   lbl_2.Visible = false;
   Controls.Add(lbl_2);
  }

  protected override void OnPreRender(EventArgs e)
  {
   if (!Page.IsPostBack)
   {
    StartVar();
    PagerBind();
   }
  }

  protected void StartVar() //初始各参数.希望只执行一次
  {
   ViewState["RecTotal"] = rectotal;
   ViewState["PageSize"] = pagesize;
   ViewState["CurPage"] = curpage;
   ViewState["Lang"] = lang ;
   ViewState["PageTotal"] = pagetotal;
  }

  #region 相关方法及事件
  public void PagerBind()
  {
   ViewState["PageTotal"]=((int)ViewState["RecTotal"]%(int)ViewState["PageSize"]>0) ? (int)ViewState["RecTotal"]/(int)ViewState["PageSize"]+1 : (int)ViewState["RecTotal"]/(int)ViewState["PageSize"];        //页总数
   if ((int)ViewState["Lang"]==0)
   {
    ((Label)Controls[0]).Text="共<font color=red>"+ViewState["RecTotal"].ToString()+"</font>条信息 <font color=red>"+ViewState["PageSize"].ToString()+"</font>条/页 当前<font color=red>"+((int)ViewState["CurPage"]+1).ToString()+"</font>/<font color=red>"+ViewState["PageTotal"].ToString()+"</font>页&nbsp;&nbsp;";
   }
   else
   {
    ((Label)Controls[0]).Text="Total<font color=red>"+ViewState["RecTotal"].ToString()+"</font>Info <font color=red>"+ViewState["PageSize"].ToString()+"</font>/Page Current<font color=red>"+((int)ViewState["CurPage"]+1).ToString()+"</font>/<font color=red>"+ViewState["PageTotal"].ToString()+"</font>Page&nbsp;&nbsp;";
    ((Label)Controls[9]).Text = "To";
    ((Label)Controls[11]).Text = "Page";
   }
   ((DropDownList)Controls[10]).Items.Clear();

   int i=0;
   do
   {
    ((DropDownList)Controls[10]).Items.Add(Convert.ToString(i+1));
    i++;
   }
   while (i<(int)ViewState["PageTotal"]);

   ((DropDownList)Controls[10]).SelectedIndex = ((DropDownList)Controls[10]).Items.IndexOf(((DropDownList)Controls[10]).Items.FindByValue(Convert.ToString((int)ViewState["CurPage"]+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 ((int)ViewState["CurPage"]==0)
   {
    ((LinkButton)Controls[1]).Enabled = false;
    ((LinkButton)Controls[3]).Enabled   = false;
    ((LinkButton)Controls[5]).Enabled  = true;
    ((LinkButton)Controls[7]).Enabled  = true;
   }
   if ((int)ViewState["CurPage"]==( (int)ViewState["PageTotal"]-1) )
   {
    ((LinkButton)Controls[1]).Enabled = true;
    ((LinkButton)Controls[3]).Enabled   = true;
    ((LinkButton)Controls[5]).Enabled  = false;
    ((LinkButton)Controls[7]).Enabled  = false;
   }
   if (( (int)ViewState["PageTotal"]-1) == 0 )
   {
    ((LinkButton)Controls[1]).Enabled = false;
    ((LinkButton)Controls[3]).Enabled   = false;
    ((LinkButton)Controls[5]).Enabled  = false;
    ((LinkButton)Controls[7]).Enabled  = false;
   }

   CurPage  = (int)ViewState["CurPage"];
   RecTotal = (int)ViewState["RecTotal"];
   PageSize = (int)ViewState["PageSize"];
   CurPage  = (int)ViewState["CurPage"];
   Lang  = (int)ViewState["Lang"];
   PageTotal = (int)ViewState["PageTotal"];
  }


  private void drop_CurPage_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   ViewState["CurPage"] = ((DropDownList)Controls[10]).SelectedIndex;
   PagerBind();

   if (PageChanged != null)
    temp_Click(this, EventArgs.Empty);
  }

  private void btn_First_Click(object sender, System.EventArgs e)
  {
   ViewState["CurPage"] = 0;
   PagerBind();

   if (PageChanged != null)
    temp_Click(this, EventArgs.Empty);
  }

  private void btn_Pre_Click(object sender, System.EventArgs e)
  {
   ViewState["CurPage"] = (int)ViewState["CurPage"]-1;
   PagerBind();

   if (PageChanged != null)
    temp_Click(this, EventArgs.Empty);
  }

  private void btn_Next_Click(object sender, System.EventArgs e)
  {
   ViewState["CurPage"] = (int)ViewState["CurPage"]+1;
   PagerBind();

   if (PageChanged != null)
    temp_Click(this, EventArgs.Empty);
  }

  private void btn_Last_Click(object sender, System.EventArgs e)
  {
   ViewState["CurPage"] = (int)ViewState["PageTotal"]-1;
   PagerBind();

   if (PageChanged != null)
    temp_Click(this, EventArgs.Empty);
  }
  #endregion
 }
}
----------------------------------end------------------------------------------------------------------------

---------WebForm1.aspx  控件测试页面文件-------------------------------------------------------------
 <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="MyWebControll.WebForm1" %>
<%@ Register TagPrefix="cc1" Namespace="C_Pager" Assembly="C_Pager" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <cc1:pager id="Pager1" runat="server" PageSize="4"></cc1:pager>
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 56px" runat="server"
     Width="600px" Height="8px"></asp:DataGrid></FONT></form>
 </body>
</HTML>
---------------------------------end------------------------------------------------------

------------WebForm1.aspx.cs  页面源文件---------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace MyWebControll
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected C_Pager.Pager Pager1;
  protected OleDbConnection conn;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected OleDbCommand cmd;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面

   conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("DataBase\\TestDataBase.mdb"));
   if(!Page.IsPostBack)
   {
    DataBind(0);
   }
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Pager1.PageChanged += new System.EventHandler(this.Change);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void DataBind(int CurPage)  //数据绑定方法
  {
   cmd=new OleDbCommand("select * from news order by id ",conn);
   OleDbDataAdapter adapter=new OleDbDataAdapter(cmd);
   DataSet ds=new DataSet();
   adapter.Fill(ds,Pager1.PageSize*CurPage,Pager1.PageSize,"news");

   DataGrid1.DataSource=ds.Tables["news"];
   DataGrid1.DataBind();

   cmd=new OleDbCommand("select count(id) from news",conn);
   conn.Open();
   Pager1.RecTotal = (int)cmd.ExecuteScalar();
   Pager1.DataBind();
   conn.Close();
  }

  private void Change(object sender, System.EventArgs e)
  {
   DataBind(Pager1.CurPage); //得到当前页记录集绑定到DataGird表上
  }

 }
}

---------------------------------------------end--------------------------------------------------

如果哪位朋友需要项目文件的话,也可以发邮件到我的邮箱向我索取的在(.net2003)已调试通过的项目代码(和下面的源代码一样)
我的邮箱是:[email protected]

本文地址:http://com.8s8s.com/it/it8033.htm