datagrid分页问题(前后跳页)《控件版继承webcontrol》

之前是一个用户控件改写的,最近自己要用,就做了一个,效果还可以,以下代码参考:

using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebControls
{

 [DefaultProperty("Paging"), DefaultEvent("PageIndexChanged"), ControlBuilderAttribute(typeof(DataGridPageBuilder)),
  ParseChildren(false), PersistChildren(false), Designer(typeof(DataGridPageDesigner)),
  ToolboxData("<{0}:DataGridPage runat=server>")]
 public class DataGridPage : WebControl, IPostBackEventHandler
 {
  private string tmpID;
  private Unit tmpFirstImagebuttonCellWidth;
  private Unit tmpPreviousImagebuttonCellWidth;
  private Unit tmpNextImagebuttonCellWidth;
  private Unit tmpLastImagebuttonCellWidth;
  private Unit tmpPageTextLabelCellWidth;
  private Unit tmpCurrentPageTextCellWidth;
  private Unit tmpShowPageRolbtnCellWidth;
  private string tmpDataGridId;
  private ImageButtonEnable conFirstImagebutton;
  private ImageButtonEnable conPreviousImagebutton;
  private ImageButtonEnable conNextImagebutton;
  private ImageButtonEnable conLastImagebutton;
  private Label conPageTextLabel;
  private TextBox conCurPageTextBox;
  private ImageButtonEnable conShowPageImageButtonEnable;
  private DataGrid conDataGrid;
  public event EventHandler PageIndexChanged;
  public event EventHandler TextBoxEnterReturn;
  public event EventHandler ButtonClicked;
  

#region "property"
  [Category("Data"), DefaultValue(""), NotifyParentProperty(true), Description("datagrid")]
  public string TargetDataGrid
  {
   get
   {
    return tmpDataGridId;
   }
   set
   {
    tmpDataGridId = value;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("最前页")]
  public ImageButtonEnable SubFirstImageButton
  {
   get
   {
    this.EnsureChildControls();
    return conFirstImagebutton;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("前一页")]
  public ImageButtonEnable SubPreviousImageButton
  {
   get
   {
    this.EnsureChildControls();
    return conPreviousImagebutton;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("下一页")]
  public TextBox SubCurPageTextBox
  {
   get
   {
    this.EnsureChildControls();
    return conCurPageTextBox;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("页数")]
  public Label SubPageTextLabel
  {
   get
   {
    this.EnsureChildControls();
    return conPageTextLabel;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("下一页")]
  public ImageButtonEnable SubNextImageButton
  {
   get
   {
    this.EnsureChildControls();
    return conNextImagebutton;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("最后页")]
  public ImageButtonEnable SubLastImageButton
  {
   get
   {
    this.EnsureChildControls();
    return conLastImagebutton;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("表示页")]
  public ImageButtonEnable SubShowPageImageButtonEnable
  {
   get
   {
    this.EnsureChildControls();
    return conShowPageImageButtonEnable;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "19px"), NotifyParentProperty(true), Description("最前页的宽度")]
  public Unit FirstImagebuttonCellWidth
  {
   get
   {
    return tmpFirstImagebuttonCellWidth;
   }
   set
   {
    tmpFirstImagebuttonCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "12px"), NotifyParentProperty(true), Description("前一页的宽度")]
  public Unit PreviousImagebuttonCellWidth
  {
   get
   {
    return tmpPreviousImagebuttonCellWidth;
   }
   set
   {
    tmpPreviousImagebuttonCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "31px"), NotifyParentProperty(true), Description("当前页")]
  public Unit CurPageTextBoxCellWidth
  {
   get
   {
    return tmpCurrentPageTextCellWidth;
   }
   set
   {
    tmpCurrentPageTextCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "24px"), NotifyParentProperty(true), Description("页")]
  public Unit PageTextLabelCellWidth
  {
   get
   {
    return tmpPageTextLabelCellWidth;
   }
   set
   {
    tmpPageTextLabelCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "12px"), NotifyParentProperty(true), Description("下一页的宽度")]
  public Unit NextImagebuttonCellWidth
  {
   get
   {
    return tmpNextImagebuttonCellWidth;
   }
   set
   {
    tmpNextImagebuttonCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "19px"), NotifyParentProperty(true), Description("最后页的宽度")]
  public Unit LastImagebuttonCellWidth
  {
   get
   {
    return tmpLastImagebuttonCellWidth;
   }
   set
   {
    tmpLastImagebuttonCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "0px"), NotifyParentProperty(true), Description("表示页的宽度")]
  public Unit ShowPageRolbtnCellWidth
  {
   get
   {
    return tmpShowPageRolbtnCellWidth;
   }
   set
   {
    tmpShowPageRolbtnCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "0px"), NotifyParentProperty(true), Description("数据源")]
  public DataTable DataBindDataSource
  {
   get
   {
    return (DataTable)ViewState["DataBindDataSource"];
   }
   set
   {
    ViewState["DataBindDataSource"] = value;
   }
  }

  #region "PProperty"
  [NotifyParentProperty(true)]
  public override string ID
  {
   get
   {
    
    tmpID = (string)((ViewState["id"] == null) ? base.ID: ViewState["id"]);
    conFirstImagebutton.ID = tmpID + "_FirstImageButton";
    conPreviousImagebutton.ID = tmpID + "_PreviousImageButton";
    conPageTextLabel.ID = tmpID + "_PageTextLabel";
    conNextImagebutton.ID = tmpID + "_NextImageButton";
    conLastImagebutton.ID = tmpID + "_LastImageButton";
    conCurPageTextBox.ID = tmpID + "_CurPageTextBox";
    this.conShowPageImageButtonEnable.ID = tmpID + "_ShowPageImageButtonEnable";
    return tmpID;
   }
   set
   {
    ViewState["id"] = value;
   }
  }

  [NotifyParentProperty(true)]
  public override System.Drawing.Color BackColor
  {
   get
   {
    return base.BackColor;
   }
   set
   {
    base.BackColor = value;
    if (!(conFirstImagebutton == null))
    {
     conFirstImagebutton.BackColor = value;
    }
    if (!(conPreviousImagebutton == null))
    {
     conPreviousImagebutton.BackColor = value;
    }
    if (!(conPageTextLabel == null))
    {
     conPageTextLabel.BackColor = value;
    }
    if (!(conNextImagebutton == null))
    {
     conNextImagebutton.BackColor = value;
    }
    if (!(conLastImagebutton == null))
    {
     conLastImagebutton.BackColor = value;
    }
    if (!(conCurPageTextBox == null))
    {
     conCurPageTextBox.BackColor = value;
    }
    if (!(conShowPageImageButtonEnable == null))
    {
     conShowPageImageButtonEnable.BackColor = value;
    }
   }
  }

  [NotifyParentProperty(true)]
  public override System.Drawing.Color BorderColor
  {
   get
   {
    return base.BorderColor;
   }
   set
   {
    base.BorderColor = value;
   }
  }

  [NotifyParentProperty(true)]
  public override BorderStyle BorderStyle
  {
   get
   {
    return base.BorderStyle;
   }
   set
   {
    base.BorderStyle = value;
   }
  }

  [NotifyParentProperty(true)]
  public override Unit BorderWidth
  {
   get
   {
    return base.BorderWidth;
   }
   set
   {
    base.BorderWidth = value;
   }
  }

  [NotifyParentProperty(true)]
  public override System.Drawing.Color ForeColor
  {
   get
   {
    return base.ForeColor;
   }
   set
   {
    base.ForeColor = value;
    if (!(conFirstImagebutton == null))
    {
     conFirstImagebutton.ForeColor = value;
    }
    if (!(conPreviousImagebutton == null))
    {
     conPreviousImagebutton.ForeColor = value;
    }
    if (!(conPageTextLabel == null))
    {
     conPageTextLabel.ForeColor = value;
    }
    if (!(conNextImagebutton == null))
    {
     conNextImagebutton.ForeColor = value;
    }
    if (!(conLastImagebutton == null))
    {
     conLastImagebutton.ForeColor = value;
    }
    if (!(conCurPageTextBox == null))
    {
     conCurPageTextBox.ForeColor = value;
    }
    if (!(conShowPageImageButtonEnable == null))
    {
     conShowPageImageButtonEnable.ForeColor = value;
    }
   }
  }

  [NotifyParentProperty(true)]
  public override Unit Width
  {
   get
   {
    return base.Width;
   }
   set
   {
    base.Width = value;
   }
  }

  [NotifyParentProperty(true)]
  public override Unit Height
  {
   get
   {
    return base.Height;
   }
   set
   {
    base.Height = value;
   }
  }

  [NotifyParentProperty(true)]
  public override bool Enabled
  {
   get
   {
    return base.Enabled;
   }
   set
   {
    base.Enabled = value;
   }
  }

  [NotifyParentProperty(true)]
  public override bool Visible
  {
   get
   {
    return base.Visible;
   }
   set
   {
    base.Visible = value;
   }
  }

  [NotifyParentProperty(true)]
  public override string CssClass
  {
   get
   {
    return base.CssClass;
   }
   set
   {
    base.CssClass = value;
    if (!(conFirstImagebutton == null))
    {
     conFirstImagebutton.CssClass = value;
    }
    if (!(conPreviousImagebutton == null))
    {
     conPreviousImagebutton.CssClass = value;
    }
    if (!(conPageTextLabel == null))
    {
     conPageTextLabel.CssClass = value;
    }
    if (!(conNextImagebutton == null))
    {
     conNextImagebutton.CssClass = value;
    }
    if (!(conLastImagebutton == null))
    {
     conLastImagebutton.CssClass = value;
    }
    if (!(conCurPageTextBox == null))
    {
     conCurPageTextBox.CssClass = value;
    }
    if (!(conShowPageImageButtonEnable == null))
    {
     conShowPageImageButtonEnable.CssClass = value;
    }
   }
  }
#endregion
#endregion

  public DataGridPage()
  {
   this.FirstImagebuttonCellWidth = Unit.Pixel(19);
   this.PreviousImagebuttonCellWidth = Unit.Pixel(12);
   this.PageTextLabelCellWidth = Unit.Pixel(24);
   this.NextImagebuttonCellWidth = Unit.Pixel(12);
   this.LastImagebuttonCellWidth = Unit.Pixel(19);
   this.CurPageTextBoxCellWidth = Unit.Pixel(31);
   this.ShowPageRolbtnCellWidth = Unit.Pixel(0);
  }

  private bool IsDesignMode()
  {
   if ((HttpContext.Current == null))
   {
    return true;
   }
   else
   {
    return false;
   }
  }

  protected override void CreateChildControls()
  {
   if (conFirstImagebutton == null)
   {
    conFirstImagebutton = new ImageButtonEnable();
   }
   if (conPreviousImagebutton == null)
   {
    conPreviousImagebutton = new ImageButtonEnable();
   }
   if (conCurPageTextBox == null)
   {
    conCurPageTextBox = new TextBox();
    conCurPageTextBox.Width = Unit.Pixel(28);
    conCurPageTextBox.Text = "1";
   }
   if (conPageTextLabel == null)
   {
    conPageTextLabel = new Label();
    conPageTextLabel.Text = " / 1";
   }
   if (conNextImagebutton == null)
   {
    conNextImagebutton = new ImageButtonEnable();
   }
   if (conLastImagebutton == null)
   {
    conLastImagebutton = new ImageButtonEnable();
   }
   if (conShowPageImageButtonEnable == null)
   {
    conShowPageImageButtonEnable = new ImageButtonEnable();
   }

   this.SubFirstImageButton.Click += new System.Web.UI.ImageClickEventHandler(this.conFirstImagebutton_Click);
   this.SubNextImageButton.Click += new System.Web.UI.ImageClickEventHandler(this.conNextImagebutton_Click);
   this.SubPreviousImageButton.Click += new System.Web.UI.ImageClickEventHandler(this.conPreviousImagebutton_Click);
   this.SubLastImageButton.Click += new System.Web.UI.ImageClickEventHandler(this.conLastImagebutton_Click);
   this.SubShowPageImageButtonEnable.Click  += new System.Web.UI.ImageClickEventHandler(this.conShowPageRolloverImageButton_Click);

   SetChildLayout();
  }
  
  private void SetChildLayout()
  {
   Table htmlTable;
   TableRow htmlTableRow;
   TableRow nullTableRow;
   htmlTable = new Table();
   htmlTableRow = new TableRow();
   htmlTable.Rows.Add(htmlTableRow);
   htmlTable.BorderWidth = Unit.Pixel(0);
   htmlTable.CellPadding = 0;
   htmlTable.CellSpacing = 0;
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[0].Width = tmpFirstImagebuttonCellWidth;
   htmlTableRow.Cells[0].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[0].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[0].Controls.Add(conFirstImagebutton);
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[1].Width = this.tmpPreviousImagebuttonCellWidth;
   htmlTableRow.Cells[1].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[1].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[1].Controls.Add(conPreviousImagebutton);
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[2].RowSpan = 2;
   htmlTableRow.Cells[2].Width = this.tmpCurrentPageTextCellWidth;
   htmlTableRow.Cells[2].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[2].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[2].Controls.Add(this.conCurPageTextBox);
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[3].Width = this.tmpPageTextLabelCellWidth;
   htmlTableRow.Cells[3].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[3].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[3].Controls.Add(conPageTextLabel);
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[4].Width = this.tmpNextImagebuttonCellWidth;
   htmlTableRow.Cells[4].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[4].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[4].Controls.Add(conNextImagebutton);
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[5].Width = this.tmpLastImagebuttonCellWidth;
   htmlTableRow.Cells[5].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[5].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[5].Controls.Add(conLastImagebutton);
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[6].Width = this.tmpLastImagebuttonCellWidth;
   htmlTableRow.Cells[6].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[6].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[6].Controls.Add(this.conShowPageImageButtonEnable);
   nullTableRow = new TableRow();
   htmlTable.Rows.Add(nullTableRow);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[0].Height = Unit.Pixel(2);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[1].Height = Unit.Pixel(2);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[2].Height = Unit.Pixel(2);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[3].Height = Unit.Pixel(2);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[4].Height = Unit.Pixel(2);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[5].Height = Unit.Pixel(2);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[6].Height = Unit.Pixel(2);
   base.Controls.Add(htmlTable);
  }
  protected override void OnLoad(EventArgs e)
  {
   Paging_Load(this,e);
   base.OnLoad (e);
  }

  protected override void Render(HtmlTextWriter writer)
  {
   this.EnsureChildControls();

   if (IsDesignMode())
   {
    base.Controls.Clear();
    SetChildLayout();
   }
  
   base.Render(writer);
  }

  protected override void AddParsedSubObject(object obj)
  {
   string[] idName;
   string tmp;

   if ((obj) is ImageButtonEnable)
   {
    idName = ((ImageButtonEnable)obj).ID.Split(((string)("_")).ToCharArray());
    tmp=idName[idName.Length -1].ToString();
    
    if (tmp.Equals("FirstImageButton"))
    {
     this.conFirstImagebutton = (ImageButtonEnable)obj;
    }
    if (tmp.Equals("PreviousImageButton"))
    {
     this.conPreviousImagebutton = (ImageButtonEnable)obj;
    }
    if (tmp.Equals("NextImageButton"))
    {
     this.conNextImagebutton = (ImageButtonEnable)obj;
    }
    if (tmp.Equals("LastImageButton"))
    {
     this.conLastImagebutton = (ImageButtonEnable)obj;
    }
   }
   if ((obj) is Label)
   {
    this.conPageTextLabel = (Label)obj;
   }
   if ((obj) is TextBox)
   {
    this.conCurPageTextBox = (TextBox)obj;
   }
   if ((obj) is ImageButtonEnable)
   {
    this.conShowPageImageButtonEnable = (ImageButtonEnable)obj;
   }

  }


  private void conShowPageRolloverImageButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   int pageNum;
   bool errFlag;
   errFlag = false;

   if (!(conCurPageTextBox.Text.Equals("")) && isNumber(conCurPageTextBox.Text))
   {
    pageNum = int.Parse(this.conCurPageTextBox.Text);

    if (pageNum < 1 | conDataGrid.PageCount < pageNum)
    {
     errFlag = true;
    }
   }
   else
   {
    pageNum=1;
    errFlag = true;
   }

   if (errFlag)
   {
    conCurPageTextBox.Text=System.Convert.ToString(conDataGrid.PageCount);
    conDataGrid.CurrentPageIndex = conDataGrid.PageCount - 1;
    ReDataBind(sender, e);
   }
   else
   {
     conDataGrid.CurrentPageIndex = pageNum - 1;
     ReDataBind(sender, e);
   }

   if (((Control)(sender)) is ImageButtonEnable)
   {
    if (ButtonClicked != null)
    {
     ButtonClicked(sender, e);
    }
   }
   else if (((Control)(sender)) is TextBox)
   {
    if (TextBoxEnterReturn != null)
    {
     TextBoxEnterReturn(sender, e);
    }
   }
  }

  bool isNumber(string strValue)
  {
   int intLoop;
   int intAsc;


   for (intLoop = 0; intLoop < strValue.Length; intLoop++)
   {
    intAsc = (int)strValue[intLoop];

    if (intAsc < 48 | 57 < intAsc)
    {
     return false;
    }
   }
   return true;
  }

  public void RaisePostBackEvent(string eventArgument)
  {
   System.Web.UI.ImageClickEventArgs e1 = new ImageClickEventArgs(0, 0);

   if (eventArgument.Equals(this.ID + "_EnterReturn"))
   {
    conShowPageRolloverImageButton_Click(conCurPageTextBox, e1);
   }
  }
  
  private void conFirstImagebutton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   base.EnsureChildControls();
   conDataGrid.CurrentPageIndex = 0;
   ReDataBind(sender, e);
  }

  private void conPreviousImagebutton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   base.EnsureChildControls();
   conDataGrid.CurrentPageIndex = Math.Max(conDataGrid.CurrentPageIndex - 1, 0);
   ReDataBind(sender, e);
  }

  private void conLastImagebutton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   base.EnsureChildControls();
   conDataGrid.CurrentPageIndex = conDataGrid.PageCount - 1;
   ReDataBind(sender, e);
  }

  private void conNextImagebutton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   base.EnsureChildControls();
   conDataGrid.CurrentPageIndex = Math.Min(conDataGrid.CurrentPageIndex + 1, conDataGrid.PageCount - 1);
   ReDataBind(sender, e);
  }

  private void ReDataBind(object sender, System.EventArgs e)
  {
   
   conDataGrid.DataSource =this.DataBindDataSource;
   conDataGrid.DataBind();
   ShowDataGridInfo();

   if (PageIndexChanged != null)
   {
    PageIndexChanged(sender, e);
   }
  }

  public void ShowDataGridInfo()
  {
   int tmpPageCount;
   int tmpCurrentPageIndex;
   
   SetPaingControl();
   tmpPageCount = conDataGrid.PageCount;
   tmpCurrentPageIndex = conDataGrid.CurrentPageIndex;

   this.conCurPageTextBox.Text = System.Convert.ToString(tmpCurrentPageIndex + 1);
         
   this.conShowPageImageButtonEnable.Enabled = this.conCurPageTextBox.Enabled;
   this.conPageTextLabel.Text = " / " + tmpPageCount;
   this.conFirstImagebutton.Enabled = (conDataGrid.CurrentPageIndex != 0);
   this.conPreviousImagebutton.Enabled = (conDataGrid.CurrentPageIndex != 0);
   this.conNextImagebutton.Enabled = (conDataGrid.CurrentPageIndex != conDataGrid.PageCount - 1);
   this.conLastImagebutton.Enabled = (conDataGrid.CurrentPageIndex != conDataGrid.PageCount - 1);
  }

  private void Paging_Load(object sender, System.EventArgs e)
  {
   SetPaingControl();
   if (!(Page.IsPostBack))
   {
    ShowDataGridInfo();
   }
  }

  private void SetPaingControl()
  {
   Control ctl;
   
   base.EnsureChildControls();
   if (tmpDataGridId == null)
   {
    throw new Exception("DataGrid没设置");
    return ;
   }
    
   ctl = Page.FindControl(tmpDataGridId);
   conDataGrid = ((DataGrid)(ctl));
   
   if (conDataGrid == null)
   {
    throw new Exception(tmpDataGridId + "不存在");
    return ;
   }
   
  }


 }

}

你可能感兴趣的:(asp.net)