封装一个用户控件的pager.ascx文件供自己使用

19 篇文章 0 订阅

由于决定不使用datagrid分页,使用存储过程来分页,而且又希望自己的分页按钮达到一定的动态效果,就封装了一个pager.ascx,其中对数据库的操作采用了一个msdn上的SqlHelper类,既DAAB封装来进行。这可以从一定程度上减轻我们的代码量和效率。多余的也没什么说的,主要思路就是用一个placeholder控件来生成一个table,然后根据不同需要生成不同的控件,然后添加不同的客户端属性。目前只有首页,上一页,下一页,尾页这四个,需要的话可以自己补充。存在一定的局限性,当作一次尝试还是比较满意,目前运行正常,呵呵,以后决定用webdier的分页控件了,感觉蛮强大的。^_^
源代码和使用方法如下:


namespace WebSite.webComponents
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;
 using System.Data.SqlClient;

 /// <summary>
 ///  pager 的摘要说明。
 /// </summary>
 public class pager : System.Web.UI.UserControl
 {
  protected System.Web.UI.WebControls.PlaceHolder PhPager;
       
  //DataGrid和DataList都继承于BaseDataList,所以用BaseDataList来保存传入的DataGrid和DataList
  private BaseDataList _controlToBind;
  private string _pagerConString;
  private string _pagerTable;
  private string _pagerOrderId;
  private int _pagerOrderType;
  private int _pageSize;
  private string _pagerCondition;
 

  SqlParameter[] parasGetRecords;
  protected System.Web.UI.WebControls.Label LbCurrent;
  protected System.Web.UI.WebControls.Label LbTotal;
  static int recordsCount;


  #region Page_Load事件
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!IsPostBack)
   {
    //这个参数设置为1,代表存储过程求相关表的记录总数
    parasGetRecords=new SqlParameter[7];
    parasGetRecords[0]=new SqlParameter("@tblName",SqlDbType.VarChar,255);
    parasGetRecords[1]=new SqlParameter("@fldName",SqlDbType.VarChar,255);
    parasGetRecords[2]=new SqlParameter("@PageIndex",SqlDbType.Int);
    parasGetRecords[3]=new SqlParameter("@IsCount",SqlDbType.Bit);
    parasGetRecords[4]=new SqlParameter("@PageSize",SqlDbType.Int);
    parasGetRecords[5]=new SqlParameter("@OrderType",SqlDbType.Int);
    parasGetRecords[6]=new SqlParameter("@strWhere",SqlDbType.VarChar,1000);

    parasGetRecords[0].Value=PagerTable;//从属性获取取值的Table名字
    parasGetRecords[1].Value=PagerOrderId;//从属性获取值设置用于排序的字段
    parasGetRecords[2].Value=1;
    parasGetRecords[3].Value=1;//设置为1,取所有记录总条数
    parasGetRecords[4].Value=PageSizeControl;
    parasGetRecords[5].Value=PagerOrderType;
    parasGetRecords[6].Value=PagerCondition;

    SqlHelperParameterCache.CacheParameterSet(PagerConString,"N_GetPageRecords",parasGetRecords);
    recordsCount=(int)SqlHelper.ExecuteScalar(PagerConString,CommandType.StoredProcedure,"N_GetPageRecords",parasGetRecords);
           
    //获取最后一页的pageIndex
    int lastPageIndex=(int)Math.Ceiling((double)recordsCount/Convert.ToDouble(parasGetRecords[4].Value));
    ViewState["LastPageIndex"]=lastPageIndex;

    this.LbCurrent.Text="第1页/";
    this.LbTotal.Text="共"+Convert.ToString(lastPageIndex)+"页";

    //设置为0,表示以后使用该参数数组的存储过程不求记录总数
    parasGetRecords[3].Value=0;
                //设置Session["PageIndex"]来保存当前分页的页数。
    Session["PageIndex"]=1;
   }
   CreatePager();

   if(this.PageSizeControl >= recordsCount)
   {
    this.PhPager.Visible=false;
    this.LbCurrent.Visible=false;
    this.LbTotal.Visible=false;
    this.LbCurrent.Style.Add("display","none");
    this.LbTotal.Style.Add("display","none");
   }
  }
  #endregion

       

  #region 创建分页控件的显示
  private void CreatePager()
  {
   Table TbPager=new Table();

   //下面两种方法都可以得到Percentage的width,比如100%

//   Unit TableUnit=new Unit((double)100,UnitType.Percentage);
   Unit TableUnit=Unit.Percentage(100);
   
   TbPager.Width=TableUnit;
   TbPager.CellSpacing=0;
   TbPager.CellSpacing=0;
   TbPager.HorizontalAlign=HorizontalAlign.Right;
   
   TableRow TrPager=new TableRow();
   TableCell TdPager=new TableCell();
   TdPager.HorizontalAlign=HorizontalAlign.Right;
   TdPager.VerticalAlign=VerticalAlign.Bottom;

   string strFirst="<Font face=webdings>9</font>";
   string strLast="<Font face=webdings>:</font>";
   string strNext="<Font face=webdings>8</font>";
   string strPrev="<Font face=webdings>7</font>";

   LinkButton PagerFirst=CreateLinkButton(strFirst,"first","首页");
   LinkButton PagerLast=CreateLinkButton(strLast,"last","尾页");
   LinkButton PagerNext=CreateLinkButton(strNext,"next","下一页");
   LinkButton PagerPrev=CreateLinkButton(strPrev,"previous","上一页");


//   Label currentIndex=CreateLabel((int)ViewState["CurrentPageIndex"],"第");
//   System.Web.UI.LiteralControl midSymbol=new System.Web.UI.LiteralControl("/");
//            Label pageTotal=CreateLabel((int)ViewState["LastPageIndex"],"共");
//            System.Web.UI.LiteralControl spacer=new System.Web.UI.LiteralControl("&nbsp;&nbsp;");
//
//   TdPager.Controls.Add(currentIndex);
//   TdPager.Controls.Add(midSymbol);
//   TdPager.Controls.Add(pageTotal);
//   TdPager.Controls.Add(spacer);

   TdPager.Controls.Add(PagerFirst);
   TdPager.Controls.Add(PagerPrev);
   TdPager.Controls.Add(PagerNext);
   TdPager.Controls.Add(PagerLast);

 

   TrPager.Controls.Add(TdPager);
   TbPager.Controls.Add(TrPager);

   this.PhPager.Controls.Add(TbPager);

  }
  #endregion

        #region 创建分页控件中LinkButton的各种动态效果和事件绑定
  private LinkButton CreateLinkButton(string strText,string strCommandName,string strTitle)
  {
   LinkButton myLinkButton=new LinkButton();
   
   
   myLinkButton.ForeColor=Color.Black;
   
   myLinkButton.CommandName=strCommandName;
   myLinkButton.Text=(strText);
   myLinkButton.ToolTip=strTitle;
   myLinkButton.Attributes.Add("onmouseover","this.style.textDecoration='underline';this.style.color='red';this.style.cursor='hand';");
   myLinkButton.Attributes.Add("onmouseout","this.style.textDecoration='none';this.style.color='black';this.style.cursor='point';");
   myLinkButton.Attributes.Add("style","text-decoration:none;font-size:15");
   myLinkButton.Click +=new EventHandler(NavigationButtonClick);
   
   return myLinkButton;

  }
  #endregion

  #region LinkButton的点击事件处理程序
  private void NavigationButtonClick(object sender, System.EventArgs e)
  {
   string direction = ((LinkButton)sender).CommandName;
   int lastPageIndex = (int)ViewState["LastPageIndex"];
   DataSet ds1;
   

   switch (direction.ToUpper())
   {
    case "FIRST" :
     parasGetRecords=SqlHelperParameterCache.GetCachedParameterSet(PagerConString,"N_GetPageRecords");
     parasGetRecords[2].Value=1;
//     ViewState["PageIndex"]=(int)parasGetRecords[2].Value;
                     Session["PageIndex"]=(int)parasGetRecords[2].Value;
      this.LbCurrent.Text="第1页/";
                   

     ds1=SqlHelper.ExecuteDataset(PagerConString,CommandType.StoredProcedure,"N_GetPageRecords",parasGetRecords);
     SqlHelperParameterCache.CacheParameterSet(PagerConString,"N_GetPageRecords",parasGetRecords);
     break;
    case "PREVIOUS" :
     parasGetRecords=SqlHelperParameterCache.GetCachedParameterSet(PagerConString,"N_GetPageRecords");
     if((int)parasGetRecords[2].Value>1)
         parasGetRecords[2].Value=(int)parasGetRecords[2].Value-1;
     else
      parasGetRecords[2].Value=1;
//
//      ViewState["PageIndex"]=(int)parasGetRecords[2].Value;
     Session["PageIndex"]=(int)parasGetRecords[2].Value;
      this.LbCurrent.Text="第"+parasGetRecords[2].Value.ToString()+"页/";
     

     ds1=SqlHelper.ExecuteDataset(PagerConString,CommandType.StoredProcedure,"N_GetPageRecords",parasGetRecords);
     SqlHelperParameterCache.CacheParameterSet(PagerConString,"N_GetPageRecords",parasGetRecords);
     break;
    case "NEXT" :
     parasGetRecords=SqlHelperParameterCache.GetCachedParameterSet(PagerConString,"N_GetPageRecords");
     if((int)parasGetRecords[2].Value<lastPageIndex)
      parasGetRecords[2].Value=(int)parasGetRecords[2].Value+1;
     else
      parasGetRecords[2].Value=lastPageIndex;

//     ViewState["PageIndex"]=(int)parasGetRecords[2].Value;
     Session["PageIndex"]=(int)parasGetRecords[2].Value;
      this.LbCurrent.Text="第"+parasGetRecords[2].Value.ToString()+"页/";
     

     ds1=SqlHelper.ExecuteDataset(PagerConString,CommandType.StoredProcedure,"N_GetPageRecords",parasGetRecords);
     SqlHelperParameterCache.CacheParameterSet(PagerConString,"N_GetPageRecords",parasGetRecords);
     break;
    case "LAST" :
     parasGetRecords=SqlHelperParameterCache.GetCachedParameterSet(PagerConString,"N_GetPageRecords");
     parasGetRecords[2].Value=lastPageIndex;

//     ViewState["PageIndex"]=(int)parasGetRecords[2].Value;
     Session["PageIndex"]=(int)parasGetRecords[2].Value;
     this.LbCurrent.Text="第"+Convert.ToString(lastPageIndex)+"页/";

     ds1=SqlHelper.ExecuteDataset(PagerConString,CommandType.StoredProcedure,"N_GetPageRecords",parasGetRecords);
     SqlHelperParameterCache.CacheParameterSet(PagerConString,"N_GetPageRecords",parasGetRecords);
     break;
    default :
     ds1=null;
     break;
   }

           //从属性取出对应的DataGrid进行绑定。
     ControlToBind.DataSource=ds1.Tables[0].DefaultView;
     ControlToBind.DataBind();
   
  }

  #endregion

  #region 属性

        #region 获取或设置需要进行分页的DataGrid
  public BaseDataList ControlToBind
  {
   get
   {
    return _controlToBind;
   }
   set
   {
    _controlToBind=value;
   }
    
  }
  #endregion

  #region 获取或设置链接字符串
  public string PagerConString
  {
   get
   {
    return _pagerConString;
   }
   set
   {
    _pagerConString=value;
   }
  }
  #endregion

        #region 获取或设置存储过程需要的,对应的数据库中的表名
  public string PagerTable
  {
   get
   {
    return _pagerTable;
   }
   set
   {
             _pagerTable=value;
   }
  }
  #endregion
  
  #region 获取或设置用于排序的字段
  public string PagerOrderId
  {
   get
   {
    return _pagerOrderId;
   }
   set
   {
    _pagerOrderId=value;
   }
  }
  #endregion

        #region 获取或设置排序的类型
  public int PagerOrderType
  {
   get
   {
    return _pagerOrderType;
   }
   set
   {
    _pagerOrderType=value;
   }
  }

  #endregion

  #region 获取或设置每页大小

  public int PageSizeControl
  {
   get
   {
    return _pageSize;
   }
   set
   {
    _pageSize=value;
   }
  }
        #endregion

  #region 获取或设置选择条件
  public string PagerCondition
  {
   get
   {
    
    return _pagerCondition;
   }
   set
   {
    _pagerCondition=value;
   }
  }
  #endregion

 


  #endregion

  #region Pager用户控件使用说明
 /*
  * 这个控件作为用户控件,放在一个拥有需要分页的DataGrid、DataList等继承至BaseDataList的页面中,位置任意,它的查询操作和参数的存取都采用了SqlHelper类,该
  * 类为msdn上的一个封装。使用Session["PageIndex"]保存第几页。
传递参数和一般使用如下代码:
  *
  *
  *
  *  protected DataGrid DgJobMes;
  * protected webComponents.pager MyPager;
  *  
  * 
  * private void Page_Load(object sender, System.EventArgs e)
  * {
  *  // 在此处放置用户代码以初始化页面
  *  if(!IsPostBack)
  *  {
     *         //这里绑定页面的第一页
     *    
     * }
     *
     *   //给分页控件三个必须属性赋值---这里的这三个属性一定要在if(!IsPostBack)外面进行赋值。
     *
     *    MyPager.PagerOrderId="Doc_Id";
           MyPager.PagerOrderType=1;
           MyPager.PagerTable="N_Documents";
           MyPager.PageSizeControl=12;
           MyPager.PagerConString=SqlHelper.SqlCon;
           MyPager.ControlToBind=this.DgDocDetail;     
     * }
     * 
  */
  #endregion
       

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

  }
  #endregion
 }
}


 

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值