自定义分页组件

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

namespace HKH.Control.Web
{
 
 /// <summary>
 /// 显示样式,可以用或连接,组合显示
 /// </summary>
 public enum ShowType
 {
  ShowFistLastBtn = 1,   //第一页/最后一页按钮
  ShowPreNextBtn = 2,    //上一页/下一页按钮
  ShowRecorderInfo = 4,   //总记录数信息
  ShowPageInfo = 8,    //总页数信息
  ShowPageNumber = 16,   //页码导航块
  ShowPageRowCount = 32,   //每页行数
  ShowGoPage = 64     //GO页码定位
 }

 /// <summary>
 /// PageControl 的摘要说明。
 /// 继承ListControl是为了应用它的Items属性
 /// 本应继承WebControl
 /// </summary>
 [DefaultProperty("Text"),
 ToolboxData("<{0}:PageControl runat=server></{0}:PageControl>")]
 public class PageControl : System.Web.UI.WebControls.ListControl,INamingContainer
 {

  #region 设定该控件所拥有的子控件

  private DropDownList rowCount=new DropDownList();   //设置每页行数

  private LinkButton lbtn_first=new LinkButton();    //页码导航块第一页
  private Button btn_first = new Button();     //第一页按钮
  private Button btn_pre=new Button();      //上一页按钮
  private Button btn_next=new Button();      //下一页按钮
  private Button btn_last = new Button();      //最后一页按钮
  private LinkButton lbtn_last=new LinkButton();    //页码导航块最后一页
  private TextBox txt_GoWhere=new TextBox();     //定位页码文本框
  private Button btn_go=new Button();       //转到按钮
  private Label lb_Info=new Label();

  //以下10个为定位按钮
  private LinkButton index_1=new LinkButton();
  private LinkButton index_2=new LinkButton();
  private LinkButton index_3=new LinkButton();
  private LinkButton index_4=new LinkButton();
  private LinkButton index_5=new LinkButton();
  private LinkButton index_6=new LinkButton();
  private LinkButton index_7=new LinkButton();
  private LinkButton index_8=new LinkButton();
  private LinkButton index_9=new LinkButton();
  private LinkButton index_10=new LinkButton();

  private LinkButton section_pre=new LinkButton();   //上一页块
  private LinkButton section_next=new LinkButton();   //下一页块

  #endregion

  #region 设定该控件的公有属性 

  #region 页面构成
  
  /// <summary>
  /// 是否显示第一页/最后一页按钮
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public bool ShowFistLastBtn    
  {
   get
   {
    return this.HasStyle(ShowType.ShowFistLastBtn);
   }
   set
   {
    if (value)
    {
     this.ShowInfo = this.ShowInfo | (int)ShowType.ShowFistLastBtn;
    }
    else
    {
     this.ShowInfo = this.ShowInfo & (~(int)ShowType.ShowFistLastBtn);
    }
   }
  }

  /// <summary>
  /// 是否显示上一页/下一页按钮
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public bool ShowPreNextBtn    
  {
   get
   {
    return this.HasStyle(ShowType.ShowPreNextBtn);
   }
   set
   {
    if (value)
    {
     this.ShowInfo = this.ShowInfo | (int)ShowType.ShowPreNextBtn;
    }
    else
    {
     this.ShowInfo = this.ShowInfo & (~(int)ShowType.ShowPreNextBtn);
    }
   }
  }

  /// <summary>
  /// 是否显示总记录数信息
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public bool ShowRecorderInfo   
  {
   get
   {
    return this.HasStyle(ShowType.ShowRecorderInfo);
   }
   set
   {
    if (value)
    {
     this.ShowInfo = this.ShowInfo | (int)ShowType.ShowRecorderInfo;
    }
    else
    {
     this.ShowInfo = this.ShowInfo & (~(int)ShowType.ShowRecorderInfo);
    }
   }
  }

  /// <summary>
  /// 是否显示总页数信息
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public bool ShowPageInfo    
  {
   get
   {
    return this.HasStyle(ShowType.ShowPageInfo);
   }
   set
   {
    if (value)
    {
     this.ShowInfo = this.ShowInfo | (int)ShowType.ShowPageInfo;
    }
    else
    {
     this.ShowInfo = this.ShowInfo & (~(int)ShowType.ShowPageInfo);
    }
   }
  }

  /// <summary>
  /// 是否显示页码导航块
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public bool ShowPageNumber    
  {
   get
   {
    return this.HasStyle(ShowType.ShowPageNumber);
   }
   set
   {
    if (value)
    {
     this.ShowInfo = this.ShowInfo | (int)ShowType.ShowPageNumber;
    }
    else
    {
     this.ShowInfo = this.ShowInfo & (~(int)ShowType.ShowPageNumber);
    }
   }
  }

  /// <summary>
  /// 是否显示每页行数
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public bool ShowPageRowCount   
  {
   get
   {
    return this.HasStyle(ShowType.ShowPageRowCount);
   }
   set
   {
    if (value)
    {
     this.ShowInfo = this.ShowInfo | (int)ShowType.ShowPageRowCount;
    }
    else
    {
     this.ShowInfo = this.ShowInfo & (~(int)ShowType.ShowPageRowCount);
    }
   }
  }

  /// <summary>
  /// 是否显示GO页码定位
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public bool ShowGoPage     
  {
   get
   {
    return this.HasStyle(ShowType.ShowGoPage);
   }
   set
   {
    if (value)
    {
     this.ShowInfo = this.ShowInfo | (int)ShowType.ShowGoPage;
    }
    else
    {
     this.ShowInfo = this.ShowInfo & (~(int)ShowType.ShowGoPage);
    }
   }
  }
  
  /// <summary>
  /// 获取或设置显示样式
  /// </summary>
  public int ShowInfo
  {
   get
   {
    if(ViewState["_ShowAmount"]!=null)
    {
     return (int)ViewState["_ShowAmount"];
    }
    else
    {
     return 126;
    }
    
   }
   set
   {
    ViewState["_ShowAmount"]= value;
   }
  }

  #endregion

  #region 控件中各部分的样式

  /// <summary>
  /// 获取或设置按钮样式
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public string Button_CSS
  {
   get
   {
    if(ViewState["_ButtonCSS"]!=null)
    {
     return (string)ViewState["_ButtonCSS"];
    }
    else
    {
     return "";
    }
    
   }

   set
   {
    ViewState["_ButtonCSS"]= value;
   }
  }

  /// <summary>
  /// 获取或设置定位按钮样式
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public string Go_CSS
  {
   get
   {
    if(ViewState["_GoCSS"]!=null)
    {
     return (string)ViewState["_GoCSS"];
    }
    else
    {
     return "";
    }
    
   }

   set
   {
    ViewState["_GoCSS"]= value;
   }
  }

  /// <summary>
  /// 获取或设置定位页码文本框样式
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public string TextBox_CSS
  {
   get
   {
    if(ViewState["_TextBoxCSS"]!=null)
    {
     return (string)ViewState["_TextBoxCSS"];
    }
    else
    {
     return "";
    }
    
   }

   set
   {
    ViewState["_TextBoxCSS"]= value;
    //Set_Style();
   }
  }

  /// <summary>
  /// 获取或设置标签样式
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public string Label_CSS
  {
   get
   {
    if(ViewState["_LabelCSS"]!=null)
    {
     return (string)ViewState["_LabelCSS"];
    }
    else
    {
     return "";
    }
    
   }

   set
   {
    ViewState["_LabelCSS"]= value;
   }
  }

  /// <summary>
  /// 获取或设置当前页码样式
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public string Selected_CSS
  {
   get
   {
    if(ViewState["_SelectedCSS"]!=null)
    {
     return (string)ViewState["_SelectedCSS"];
    }
    else
    {
     return "";
    }
    
   }

   set
   {
    ViewState["_SelectedCSS"]= value;
   }
  }

  /// <summary>
  /// 获取或设置第一页样式
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public string FirstPage_CSS
  {
   get
   {
    if(ViewState["_FirstPageCSS"]!=null)
    {
     return (string)ViewState["_FirstPageCSS"];
    }
    else
    {
     return "";
    }
    
   }

   set
   {
    ViewState["_FirstPageCSS"]= value;
   }
  }

  /// <summary>
  /// 获取或设置最后一页样式
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public string LastPage_CSS
  {
   get
   {
    if(ViewState["_LastPageCSS"]!=null)
    {
     return (string)ViewState["_LastPageCSS"];
    }
    else
    {
     return "";
    }
    
   }

   set
   {
    ViewState["_LastPageCSS"]= value;
   }
  }

  /// <summary>
  /// 获取或设置上一节样式
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public string PreSection_CSS
  {
   get
   {
    if(ViewState["_PreSectionCSS"]!=null)
    {
     return (string)ViewState["_PreSectionCSS"];
    }
    else
    {
     return "";
    }
    
   }

   set
   {
    ViewState["_PreSectionCSS"]= value;
   }
  }
  
  /// <summary>
  /// 获取或设置下一节样式
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public string NextSection_CSS
  {
   get
   {
    if((string)ViewState["_NextSectionCSS"]!=null)
    {
     return (string)ViewState["_NextSectionCSS"];
    }
    else
    {
     return "";
    }
    
   }

   set
   {
    ViewState["_NextSectionCSS"]= value;
   }
  }

  #region 页行数

  /// <summary>
  /// 获取或设置页行数选项
  /// </summary>
  [Bindable(true),
  Category("分页控件")
  ]
  public override ListItemCollection Items
  {
   get
   {
    return rowCount.Items;
   }
  }


  #endregion

  #endregion

  #region 块索引号
  [Bindable(true),
  Category("分页控件")
  ]
  public int Section_Index
  {
   get
   {
    if(ViewState["_SectionIndex"]!=null)
    {
     return Convert.ToInt16(ViewState["_SectionIndex"]);
    }
    else
    {
     return 0;
    }
    
   }

   set
   {
    ViewState["_SectionIndex"]= value;
   }
  }
  #endregion

  #region 当前页的索引号
  [Bindable(true),
  Category("分页控件"),
  DefaultValue(1)]
  public int Current_Index
  {
   get
   {
    if(ViewState["_CurrentIndex"]!=null)
    {
     int c_Index=Convert.ToInt16(ViewState["_CurrentIndex"]);
     return c_Index < 1 ? 1 : c_Index;
    }
    else
    {
     return 1;
    }
    
   }

   set
   {
    ViewState["_CurrentIndex"]= value;
   }
  }
  #endregion

  #region 最后一页的索引号
  [Bindable(true),
  Category("分页控件")
  ]
  public int Last_Index
  {
   get
   {
    if(ViewState["_LastPageNum"]!=null)
    {
     int l_Index=Convert.ToInt16(ViewState["_LastPageNum"]);
     return l_Index < 1 ? 1 : l_Index;
    }
    else
    {
     return 1;
    }
    
   }

   set
   {
    ViewState["_LastPageNum"]= value;
   }
  }
  #endregion

  #region 总记录条数
  [Bindable(true),
  Category("分页控件")
  ]
  public int Total_Count
  {
   get
   {
    if(ViewState["_Total_Count"]!=null)
    {
     return (int)ViewState["_Total_Count"];
    }
    else
    {
     return 0;
    }
    
   }

   set
   {
    ViewState["_Total_Count"]= value;
    Calculate_LastIndex();
   }
  }
  #endregion

  #region 一页的行数
  [Bindable(true),
  Category("分页控件"),
  DefaultValue(15)]
  public int Page_Size
  {
   get
   {
    if(ViewState["_Page_Size"]!=null)
    {
     return (int)ViewState["_Page_Size"];
    }
    else
    {
     return 15;
    }
    
   }

   set
   {
    ViewState["_Page_Size"]= value;
    Calculate_LastIndex();
   }
  }
  #endregion

  #region 保存查询条件
  [Bindable(true),
  Category("分页控件")
  ]
  public string KeyWord
  {
   get
   {
    if(ViewState["_KeyWord"]!=null)
    {
     return ViewState["_KeyWord"].ToString();
    }
    else
    {
     return "";
    }
    
   }

   set
   {
    ViewState["_KeyWord"]= value;
   }
  }
  #endregion

  #endregion

  #region 控件呈现

  /// <summary>
  /// 将此控件呈现给指定的输出参数。
  /// </summary>
  /// <param name="output"> 要写出到的 HTML 编写器 </param>
  protected override void Render(HtmlTextWriter output)
  {
   Set_Style();

   output.Write("<table width=/"100%/" border=/"0/" cellspacing=/"0/" cellpadding=/"0/">");
   output.Write("<tr>");

   if( this.HasStyle(ShowType.ShowFistLastBtn))
   {
    output.Write("<td width=/"9%/" height=/"31/" align=/"left/" valign=/"center/" nowrap>");
    btn_first.RenderControl(output);
    output.Write("</td>");
   }

   if (this.HasStyle(ShowType.ShowPreNextBtn))
   {
    output.Write("<td width=/"9%/" height=/"31/" align=/"left/" valign=/"center/" nowrap>");
    btn_pre.RenderControl(output);
    output.Write("</td>");
    output.Write("<td width=/"9%/" align=/"left/" valign=/"center/" nowrap>");
    btn_next.RenderControl(output);
    output.Write("</td>");
   }

   if (this.HasStyle(ShowType.ShowFistLastBtn))
   {
    output.Write("<td width=/"9%/" height=/"31/" align=/"left/" valign=/"center/" nowrap>");
    btn_last.RenderControl(output);
    output.Write("</td>");
   }

   if (this.HasStyle( ShowType.ShowRecorderInfo))
   {
    output.Write("<td width=/"9%/" valign=/"center/" nowrap><strong>共 <span class=/"style10/">"+Total_Count.ToString()+"</span> 条记录 </strong>");
    output.Write("</td>");
   }

   if (this.HasStyle( ShowType.ShowPageInfo ))
   {
    output.Write("<td width=/"9%/" valign=/"center/" nowrap><strong>共 <span class=/"style10/">"+Last_Index.ToString()+"</span> 页 </strong>");
    output.Write("</td>");
   }

   if (this.HasStyle(ShowType.ShowPageNumber))
   {
    output.Write("<td align=/"center/" valign=/"center/" nowrap class=/"font01/">");

    #region 用表格定住1-10的按钮

    output.Write("<table height=/"31/"border=/"0/" cellspacing=/"0/" cellpadding=/"0/">");
    output.Write("<tr><td width=/"20/" align=/"center/" valign=/"center/" nowrap>");
    lbtn_first.RenderControl(output);
    output.Write("</td>");

    //如果不是第一节,显示上一节
    if(Section_Index!=0)
    {
     output.Write("<td width=/"20/" align=/"center/" valign=/"center/" nowrap>");
     section_pre.RenderControl(output);
     output.Write("</td>");
    }
   
    HandleLinkButton(ref output,1,ref index_1);
    HandleLinkButton(ref output,2,ref index_2);
    HandleLinkButton(ref output,3,ref index_3);
    HandleLinkButton(ref output,4,ref index_4);
    HandleLinkButton(ref output,5,ref index_5);
    HandleLinkButton(ref output,6,ref index_6);
    HandleLinkButton(ref output,7,ref index_7);
    HandleLinkButton(ref output,8,ref index_8);
    HandleLinkButton(ref output,9,ref index_9);
    HandleLinkButton(ref output,10,ref index_10);
   
    HandleLinkButton(ref output,11,ref section_next);

    output.Write("<td width=/"20/" align=/"center/" valign=/"center/" nowrap>");
    lbtn_last.RenderControl(output);
    output.Write("</td></tr></table>");
    #endregion

    output.Write("</td>");
    output.Write("&nbsp;&nbsp;");
   }

   #region 设置每页显示记录数

   if (this.HasStyle(ShowType.ShowPageRowCount))
   {
    output.Write("<td width=/"15%/" height=/"31/" valign=/"center/" nowrap><strong>每页 ");
    rowCount.RenderControl(output);
    output.Write(" 条记录 </strong></td>");
    output.Write("&nbsp;&nbsp;");
   }
 
   #endregion

   if (this.HasStyle(ShowType.ShowGoPage))
   {
    output.Write("<td width=/"15%/" align=/"right/" valign=/"center/" nowrap>转到:");
    txt_GoWhere.RenderControl(output);
    output.Write("&nbsp;&nbsp;");
    btn_go.RenderControl(output);
    output.Write("</td>");
   }
   
   output.Write("</tr></table>");
   
  }

  /// <summary>
  /// 生成10个按钮,做定位
  /// </summary>
  /// <param name="output"></param>
  /// <param name="Index"></param>
  /// <param name="lkButton"></param>
  private void HandleLinkButton(ref HtmlTextWriter output,int Index,ref LinkButton lkButton)
  {
   if((Section_Index*10+Index-1)*Page_Size<Total_Count)
   {
    output.Write("<td width=/"20/" align=/"center/" valign=/"center/" nowrap>");
    lkButton.RenderControl(output);
    output.Write("</td>");
   }
   else
   {
    output.Write("<td width=/"20/" align=/"center/" valign=/"center/" nowrap>");
    output.Write("&nbsp;</td>");
   }
  }
  #endregion

  #region 添加子控件。
  /// <summary>
  /// 添加子控件。
  /// </summary>
  protected override void CreateChildControls()
  {
   InitChildControl();

   #region 给子控件添加事件

   lbtn_first.Command+=new CommandEventHandler(OnPageIndexChanged);
   lbtn_first.CommandName="first";

   btn_first.Command+=new CommandEventHandler(OnPageIndexChanged);
   btn_first.CommandName="first";
   btn_pre.Command+=new CommandEventHandler(OnPageIndexChanged);
   btn_pre.CommandName="pre";
   btn_next.Command+=new CommandEventHandler(OnPageIndexChanged);
   btn_next.CommandName="next";
   btn_last.Command+=new CommandEventHandler(OnPageIndexChanged);
   btn_last.CommandName="last";
   
   lbtn_last.Command+=new CommandEventHandler(OnPageIndexChanged);
   lbtn_last.CommandName="last";
   btn_go.Command+=new CommandEventHandler(OnPageIndexChanged);
   btn_go.CommandName="goto";

   index_1.Command+=new CommandEventHandler(OnPageIndexChanged);
   index_1.CommandName="index_1";
   index_2.Command+=new CommandEventHandler(OnPageIndexChanged);
   index_2.CommandName="index_2";
   index_3.Command+=new CommandEventHandler(OnPageIndexChanged);
   index_3.CommandName="index_3";
   index_4.Command+=new CommandEventHandler(OnPageIndexChanged);
   index_4.CommandName="index_4";
   index_5.Command+=new CommandEventHandler(OnPageIndexChanged);
   index_5.CommandName="index_5";
   index_6.Command+=new CommandEventHandler(OnPageIndexChanged);
   index_6.CommandName="index_6";
   index_7.Command+=new CommandEventHandler(OnPageIndexChanged);
   index_7.CommandName="index_7";
   index_8.Command+=new CommandEventHandler(OnPageIndexChanged);
   index_8.CommandName="index_8";
   index_9.Command+=new CommandEventHandler(OnPageIndexChanged);
   index_9.CommandName="index_9";
   index_10.Command+=new CommandEventHandler(OnPageIndexChanged);
   index_10.CommandName="index_10";

   section_pre.Command+=new CommandEventHandler(OnPageIndexChanged);
   section_pre.CommandName="section_pre";
   section_next.Command+=new CommandEventHandler(OnPageIndexChanged);
   section_next.CommandName="section_next";

   rowCount.SelectedIndexChanged+=new EventHandler(rowCount_SelectedIndexChanged);
   #endregion

   Controls.Add(lbtn_first);
   Controls.Add(btn_pre);
   Controls.Add(btn_next);
   Controls.Add(lbtn_last);
   Controls.Add(btn_go);
   Controls.Add(txt_GoWhere);

   Controls.Add(index_1);
   Controls.Add(index_2);
   Controls.Add(index_3);
   Controls.Add(index_4);
   Controls.Add(index_5);
   Controls.Add(index_6);
   Controls.Add(index_7);
   Controls.Add(index_8);
   Controls.Add(index_9);
   Controls.Add(index_10);

   Controls.Add(section_pre);
   Controls.Add(section_next);

   Controls.Add(rowCount);

  }
  #endregion

  #region 初始化子控件
  protected void InitChildControl()
  {
   #region 设置下拉框

   int Select_Index=rowCount.Items.IndexOf(rowCount.Items.FindByValue(this.Page_Size.ToString()));

   //如果没有显示,则定为全部显示,此功能审用
   if(Select_Index !=-1)
   {
    rowCount.SelectedIndex=Select_Index;
   }
   else
   {
    rowCount.SelectedIndex=0;
   }

   rowCount.EnableViewState=true;
   rowCount.AutoPostBack=true;
   #endregion
  }
  #endregion

  #region 设定样式
  protected void Set_Style()
  {
   #region 设置按钮的文字

   section_pre.Text="<FONT face='webdings' color='#ff0000'>7</FONT>";
   section_next.Text="<FONT face='webdings' color='#ff0000'>8</FONT>";
   lbtn_first.Text="<FONT face='webdings' color='#ff0000'>9</FONT>";
   lbtn_first.ToolTip="首页";
   lbtn_last.Text="<FONT face='webdings' color='#ff0000'>:</FONT>";
   lbtn_last.ToolTip="末页";
   
   index_1.Text=Convert.ToString(Section_Index*10+1);
   index_2.Text=Convert.ToString(Section_Index*10+2);
   index_3.Text=Convert.ToString(Section_Index*10+3);
   index_4.Text=Convert.ToString(Section_Index*10+4);
   index_5.Text=Convert.ToString(Section_Index*10+5);
   index_6.Text=Convert.ToString(Section_Index*10+6);
   index_7.Text=Convert.ToString(Section_Index*10+7);
   index_8.Text=Convert.ToString(Section_Index*10+8);
   index_9.Text=Convert.ToString(Section_Index*10+9);
   index_10.Text=Convert.ToString(Section_Index*10+10);
   #endregion
   
   btn_go.Text=" GO";//----暂时设定为"Go"
   btn_first.Text = "第一页";
   btn_pre.Text="上一页 ";
   btn_next.Text="下一页 ";
   btn_last.Text = "最后一页";
   lbtn_first.CssClass=FirstPage_CSS;
   lbtn_last.CssClass=LastPage_CSS;
   section_pre.CssClass=PreSection_CSS;
   section_next.CssClass=NextSection_CSS;

   btn_first.CssClass=Button_CSS;
   btn_pre.CssClass=Button_CSS;
   btn_next.CssClass=Button_CSS;
   btn_last.CssClass=Button_CSS;

   btn_go.CssClass=Go_CSS;
   txt_GoWhere.CssClass=TextBox_CSS;
   
   index_1.CssClass=Label_CSS;
   index_2.CssClass=Label_CSS;
   index_3.CssClass=Label_CSS;
   index_4.CssClass=Label_CSS;
   index_5.CssClass=Label_CSS;
   index_6.CssClass=Label_CSS;
   index_7.CssClass=Label_CSS;
   index_8.CssClass=Label_CSS;
   index_9.CssClass=Label_CSS;
   index_10.CssClass=Label_CSS;
   
   #region 设置选择项的样式
   if(Selected_CSS!="")
   {
    switch(Current_Index%10)
    {
     case 1:
     {
      index_1.CssClass=Selected_CSS;
      break;
     }
     case 2:
     {
      index_2.CssClass=Selected_CSS;
      break;
     }
     case 3:
     {
      index_3.CssClass=Selected_CSS;
      break;
     }
     case 4:
     {
      index_4.CssClass=Selected_CSS;
      break;
     }
     case 5:
     {
      index_5.CssClass=Selected_CSS;
      break;
     }
     case 6:
     {
      index_6.CssClass=Selected_CSS;
      break;
     }
     case 7:
     {
      index_7.CssClass=Selected_CSS;
      break;
     }
     case 8:
     {
      index_8.CssClass=Selected_CSS;
      break;
     }
     case 9:
     {
      index_9.CssClass=Selected_CSS;
      break;
     }
     case 0:
     {
      if(Current_Index>0)
      {
       index_10.CssClass=Selected_CSS;
      }
      break;
     }
     default:
     {
      break;
     }
    }
   }
   #endregion

   #region 部分控件的宽度

   btn_pre.Width=new Unit(60,UnitType.Pixel);
   btn_next.Width=new Unit(60,UnitType.Pixel);
   txt_GoWhere.Width=new Unit(30,UnitType.Pixel);
   btn_go.Width=new Unit(30,UnitType.Pixel);

   #endregion

  }
  #endregion
  
  #region 定义控件的分页事件

  public event System.EventHandler PageBind;//数据绑定事件

  public event CommandEventHandler PageIndexChanged;//分页事件
  
  public event System.EventHandler SelCountChanged;//每页显示记录数改变事件

  /// <summary>
  /// 数据源绑定事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  public void OnPageBind(object sender, EventArgs e)
  {
   if(PageBind!=null)
   {
    PageBind(this,e);
   }
  }

  /// <summary>
  /// 选页改变事件,如无特殊处理,此事件不写将自动调用绑定事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  public void OnPageIndexChanged(object sender,CommandEventArgs e)
  {
   Page_IndexChanged(sender,e);

   if(PageIndexChanged !=null)
   {
    PageIndexChanged(this,e);
   }
   else
   {
    this.OnPageBind(this,e);
   }
  }

  /// <summary>
  /// 每页行数改变事件,如无特殊处理,此事件不写将自动调用绑定事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void rowCount_SelectedIndexChanged(object sender, EventArgs e)
  {
   int PageSize=Convert.ToInt32(((DropDownList)sender).SelectedValue);

   if(PageSize!=0)
   {
    this.Page_Size=PageSize;
   }
   else
   {
    if(this.Total_Count!=0)
     this.Page_Size=Total_Count;
    else
     this.Page_Size=100;
   }

   ResetPageControl();

   if(SelCountChanged != null)
   {
    SelCountChanged(this,e);
   }
   else
   {
    this.OnPageBind(this,e);
   }
  }

  #endregion

  #region 翻页操作
  /// <summary>
  /// 翻页处理,在此主要计算页码及节码并缓存
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  protected void Page_IndexChanged(object sender,System.Web.UI.WebControls.CommandEventArgs e)
  {
   btn_pre.Enabled=true;
   btn_next.Enabled=true;
   switch(e.CommandName)
   {
    case "section_pre":
    {

     if(Section_Index>1)
     {
      
      Section_Index=Section_Index-1;
      Current_Index=(Section_Index)*10+1;
     }
     else
     {
      Section_Index=0;
      Current_Index=1;
     }
     break;
    }
    case "section_next":
    {
     if((Section_Index+1)*10<Last_Index)
     {
      Section_Index=Section_Index+1;
      Current_Index=(Section_Index)*10+1;
     }
     break;
    }
    case "first":
    {
     Current_Index=1;
     Section_Index=0;
     break;
    }
    case "pre":
    {
     if(Current_Index>1)
     {
      Current_Index=Current_Index-1;
     }
     else
     {
      Current_Index=1;
     }
     break;
    }
    case "next":
    {
     if(Current_Index<Last_Index)
     {
      Current_Index=Current_Index+1;
     }
     else
     {
      Current_Index=Last_Index;
     }
     break;
    }
    case "last":
    {
     Current_Index=Last_Index;
     while(Last_Index>10*(Section_Index+1))
     {
      Section_Index++;
     }

     break;
    }
    case "goto":
    {
     try
     {
      int gotowhere=Convert.ToInt16(txt_GoWhere.Text);

      gotowhere=gotowhere > 1 ? gotowhere : 1;
      Current_Index=gotowhere > Last_Index ? Last_Index : gotowhere;

      Section_Index=(Current_Index%10)>0 ?Current_Index/10 : ((Current_Index/10-1)<0 ? 0 : Current_Index/10-1);
     }
     catch
     {
      
     }
     break;
    }
    default:
    {
     int custom_index=Convert.ToInt32(e.CommandName.Replace("index_",""));
     custom_index=custom_index+Section_Index*10;
     if(custom_index>=1&&custom_index<=Last_Index)
     {
      Current_Index=custom_index;
     }
     break;
    }
   }

  }
  #endregion

  #region 计算总页数
  /// <summary>
  /// 由总记录数及页面行数计算总页数
  /// </summary>
  private void Calculate_LastIndex()
  {
   if(Total_Count==0)
   {
    Last_Index=1;
   }
   decimal maybe_pagecount;
   if(Page_Size!=0)
   {
    maybe_pagecount=Convert.ToDecimal(Total_Count)/Convert.ToDecimal(Page_Size);
   }
   else
   {
    Last_Index=1;
    maybe_pagecount=0;
   }
   int temp_index=Convert.ToInt32(Math.Round(maybe_pagecount));
   if(temp_index<maybe_pagecount)
   {
    temp_index=temp_index+1;
   }
   Last_Index=temp_index;
  }
  #endregion

  #region 清空索引记录
  public void ResetPageControl()
  {
   this.Current_Index=1;
   this.Section_Index=0;
  }
  #endregion

  #region 验证样式

  /// <summary>
  /// 验证样式
  /// </summary>
  /// <param name="showType"></param>
  /// <returns></returns>
  private bool HasStyle(ShowType showType)
  {
   if ( (this.ShowInfo | (int)showType) == this.ShowInfo )
    return true;
   else
    return false;
  }
  #endregion

 }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

honkerhero

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值