熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

自定義分頁控件源碼asp.net(c#)

2022-06-13   來源: .NET編程 

  可能大家有用得著的地方發出來一起研究下代碼如下

  Pagercs  服務器控件源代碼

  using System;
    using SystemWeb;
    using SystemWebUI;
    using SystemWebUIWebControls;
    using SystemComponentModel;

  namespace C_Pager
    {
     public class Pager : Control INamingContainer
     {
      [Description(操作員選擇不同頁時產生的頁更新事件通過此事件處理把舊頁面顯示信息更新到選擇頁對應的數據信息)]
      public event EventHandler PageChanged;

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

  private string firstpagetext =<FONT face=webdings></FONT>;
      private string prevpagetext = <FONT face=webdings></FONT>;
      private string nextpagetext = <FONT face=webdings></FONT>;
      private string lastpagetext = <FONT face=webdings>:</FONT>;

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

  Pager控件使用幫助及參數說明#region Pager控件使用幫助及參數說明
      /**//*
       本控件帶有三個自定義屬性和一個方法具體如下:
       屬性:
        RecTotal 記錄總數 默認值:
        PageSize 每頁記錄數 默認值:
        CurPage  當前頁  默認值:
        Lang  版本語言 默認值: 注:為中文版為英文版
       方法:
        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();     //
       lbl_PageInfoText = 尚未綁定;
       ControlsAdd(lbl_PageInfo);

  LinkButton btn_First = new LinkButton();   //
       btn_FirstText = ViewState[FirstPageText]ToString();
       ControlsAdd(btn_First);
       btn_FirstVisible = false;
       btn_FirstClick += new SystemEventHandler(btn_First_Click);

  ControlsAdd(new LiteralControl( ));    //

  LinkButton btn_Pre = new LinkButton();    //
       btn_PreText = ViewState[PrevPageText]ToString();
       ControlsAdd(btn_Pre);
       btn_PreVisible = false;
       btn_PreClick += new SystemEventHandler(btn_Pre_Click);

  ControlsAdd(new LiteralControl( ));    //

  LinkButton btn_Next = new LinkButton();    //
       btn_NextText = ViewState[NextPageText]ToString();
       ControlsAdd(btn_Next);
       btn_NextVisible = false;
       btn_NextClick += new SystemEventHandler(btn_Next_Click);

  ControlsAdd(new LiteralControl( ));    //

  LinkButton btn_Last = new LinkButton();    //
       btn_LastText = ViewState[LastPageText]ToString();
       ControlsAdd(btn_Last);
       btn_LastVisible = false;
       btn_LastClick += new SystemEventHandler(btn_Last_Click);

  ControlsAdd(new LiteralControl(&nbsp;&nbsp;)); //

  Label lbl_ = new Label();       //
       lbl_Text = 轉到;
       lbl_Visible = false;
       ControlsAdd(lbl_);

  DropDownList drop_CurPage = new DropDownList();  //
       drop_CurPageItemsAdd();
       ControlsAdd(drop_CurPage);
       drop_CurPageAutoPostBack = true;
       drop_CurPageVisible = false;
       drop_CurPageSelectedIndexChanged += new SystemEventHandler(drop_CurPage_SelectedIndexChanged);

  Label lbl_ = new Label();       //
       lbl_Text = ;
       lbl_Visible = false;
       ControlsAdd(lbl_);
      }

  protected override void OnPreRender(EventArgs e)
      {
       if (!PageIsPostBack)
       {
        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]>) ? (int)ViewState[RecTotal]/(int)ViewState[PageSize]+ : (int)ViewState[RecTotal]/(int)ViewState[PageSize];        //頁總數
       if ((int)ViewState[Lang]==)
       {
        ((Label)Controls[])Text=共<font color=red>+ViewState[RecTotal]ToString()+</font>條信息 <font color=red>+ViewState[PageSize]ToString()+</font>條/頁 當前<font color=red>+((int)ViewState[CurPage]+)ToString()+</font>/<font color=red>+ViewState[PageTotal]ToString()+</font>頁&nbsp;&nbsp;;
       }
       else
       {
        ((Label)Controls[])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]+)ToString()+</font>/<font color=red>+ViewState[PageTotal]ToString()+</font>Page&nbsp;&nbsp;;
        ((Label)Controls[])Text = To;
        ((Label)Controls[])Text = Page;
       }
       ((DropDownList)Controls[])ItemsClear();

  int i=;
       do
       {
        ((DropDownList)Controls[])ItemsAdd(ConvertToString(i+));
        i++;
       }
       while (i<(int)ViewState[PageTotal]);

  ((DropDownList)Controls[])SelectedIndex = ((DropDownList)Controls[])ItemsIndexOf(((DropDownList)Controls[])ItemsFindByValue(ConvertToString((int)ViewState[CurPage]+)));

  ((LinkButton)Controls[])Visible = true;
       ((LinkButton)Controls[])Visible   = true;
       ((LinkButton)Controls[])Visible  = true;
       ((LinkButton)Controls[])Visible  = true;
       ((Label)Controls[])Visible   = true;
       ((Label)Controls[])Visible   = true;
       ((DropDownList)Controls[])Visible = true;

  ((LinkButton)Controls[])Enabled = true;
       ((LinkButton)Controls[])Enabled   = true;
       ((LinkButton)Controls[])Enabled  = true;
       ((LinkButton)Controls[])Enabled  = true;

  if ((int)ViewState[CurPage]==)
       {
        ((LinkButton)Controls[])Enabled = false;
        ((LinkButton)Controls[])Enabled   = false;
        ((LinkButton)Controls[])Enabled  = true;
        ((LinkButton)Controls[])Enabled  = true;
       }
       if ((int)ViewState[CurPage]==( (int)ViewState[PageTotal]) )
       {
        ((LinkButton)Controls[])Enabled = true;
        ((LinkButton)Controls[])Enabled   = true;
        ((LinkButton)Controls[])Enabled  = false;
        ((LinkButton)Controls[])Enabled  = false;
       }
       if (( (int)ViewState[PageTotal]) == )
       {
        ((LinkButton)Controls[])Enabled = false;
        ((LinkButton)Controls[])Enabled   = false;
        ((LinkButton)Controls[])Enabled  = false;
        ((LinkButton)Controls[])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 SystemEventArgs e)
      {
       ViewState[CurPage] = ((DropDownList)Controls[])SelectedIndex;
       PagerBind();

  if (PageChanged != null)
        temp_Click(this EventArgsEmpty);
      }

  private void btn_First_Click(object sender SystemEventArgs e)
      {
       ViewState[CurPage] = ;
       PagerBind();

  if (PageChanged != null)
        temp_Click(this EventArgsEmpty);
      }

  private void btn_Pre_Click(object sender SystemEventArgs e)
      {
       ViewState[CurPage] = (int)ViewState[CurPage];
       PagerBind();

  if (PageChanged != null)
        temp_Click(this EventArgsEmpty);
      }

  private void btn_Next_Click(object sender SystemEventArgs e)
      {
       ViewState[CurPage] = (int)ViewState[CurPage]+;
       PagerBind();

  if (PageChanged != null)
        temp_Click(this EventArgsEmpty);
      }

  private void btn_Last_Click(object sender SystemEventArgs e)
      {
       ViewState[CurPage] = (int)ViewState[PageTotal];
       PagerBind();

  if (PageChanged != null)
        temp_Click(this EventArgsEmpty);
      }
      #endregion
     }
    }

  WebFormaspx  自定義分頁控件測試頁面

  具體效果展示頁面x

  <%@ Page language=c# Codebehind=WebFormaspxcs AutoEventWireup=false Inherits=MyWebControllWebForm %>
    <%@ Register TagPrefix=cc Namespace=C_Pager Assembly=C_Pager %>
    <!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN >
    <HTML>
     <HEAD>
      <title>WebForm</title>
      <meta content=Microsoft Visual Studio NET name=GENERATOR>
      <meta content=C# name=CODE_LANGUAGE>
      <meta content=JavaScript name=vs_defaultClientScript>
      <meta content= name=vs_targetSchema>
     </HEAD>
     <body MS_POSITIONING=GridLayout>
      <form id=Form method=post runat=server>
       <FONT face=宋體>
        <cc:pager id=Pager runat=server PageSize=></cc:pager>
        <asp:DataGrid id=DataGrid runat=server
         Width=px Height=px></asp:DataGrid></FONT></form>
     </body>
    </HTML>

  WebFormaspxcs  測試頁面源碼

    using System;
    using SystemCollections;
    using SystemComponentModel;
    using SystemData;
    using SystemDrawing;
    using SystemWeb;
    using SystemWebSessionState;
    using SystemWebUI;
    using SystemWebUIWebControls;
    using SystemWebUIHtmlControls;
    using SystemDataSqlClient;
    using SystemDataOleDb;

  namespace MyWebControll
    {
     /**//// <summary>
     /// WebForm 的摘要說明
     /// </summary>
     public class WebForm : SystemWebUIPage
     {
      protected C_PagerPager Pager;
      protected OleDbConnection conn;
      protected SystemWebUIWebControlsDataGrid DataGrid;
      protected OleDbCommand cmd;

  private void Page_Load(object sender SystemEventArgs e)
      {
       // 在此處放置用戶代碼以初始化頁面

  conn=new OleDbConnection(Provider=MicrosoftJetOLEDB;Data Source=+ServerMapPath(DataBase\TestDataBasemdb));
       if(!PageIsPostBack)
       {
        DataBind();
       }
      }

  Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼
      override protected void OnInit(EventArgs e)
      {
       //
       // CODEGEN: 該調用是 ASPNET Web 窗體設計器所必需的
       //
       InitializeComponent();
       baseOnInit(e);
      }

  /**//// <summary>
      /// 設計器支持所需的方法 不要使用代碼編輯器修改
      /// 此方法的內容
      /// </summary>
      private void InitializeComponent()
      {
       thisPagerPageChanged += new SystemEventHandler(thisChange);//這是必須的
       thisLoad += new SystemEventHandler(thisPage_Load);

  }
      #endregion

  private void DataBind(int CurPage)  //數據綁定方法
      {
       cmd=new OleDbCommand(select * from news order by id desc conn);
       OleDbDataAdapter adapter=new OleDbDataAdapter(cmd);
       DataSet ds=new DataSet();
       adapterFill(dsPagerPageSize*CurPagePagerPageSizenews);

  DataGridDataSource=dsTables[news];
       DataGridDataBind();

  cmd=new OleDbCommand(select count(id) from newsconn);
       connOpen();
       PagerRecTotal = (int)cmdExecuteScalar();
       PagerDataBind();
       connClose();
      }

  private void Change(object sender SystemEventArgs e)
      {
       DataBind(PagerCurPage); //得到當前頁記錄集綁定到DataGird表上
      }

  }
    }


From:http://tw.wingwit.com/Article/program/net/201311/11748.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.