当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

自定义分页控件源码asp.net(c#)

欢迎进入.NET社区论坛,与200万技术人员互动交流 >>进入

    可能大家有用得着的地方,发出来一起研究下,代码如下:

    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);
       }
      }

      Pager控件使用帮助及参数说明#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;
      }

相关内容
赞助商链接