DataGrid自定义分页控件WebControl

用了DataGrid控件很长时间,觉得分页功能有限,比如,没有共有多少条记录的提示、定位到底第几页等等,可以用WebControl 模拟出来,但是每次用都需要拷贝同样的代码很繁琐,本人很懒,所决定写一个控件来解决,命名为DataGridNavigator;
代码如下:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Collections;
using System.Data;
using System.Text;
using System.Reflection;

namespace HTCL.DataControl
{
    
/// <summary>
    
/// OnClick事件参数。
    
/// </summary>

    public class DataNavigatorClickEventArgs : EventArgs
    
{
        
int _currentpageindex;

        
/// <summary>
        
/// DataGrid当前页索引号。
        
/// </summary>

        public int CurrentPageIndex
        
{
            
get return _currentpageindex; }
        }


        
/// <summary>
        
/// 初始化一个新的DataNavigatorClickEventArgs实例。
        
/// </summary>
        
/// <param name="currentpageindex">当前页索引号</param>

        public DataNavigatorClickEventArgs(int currentpageindex)
        
{
            _currentpageindex 
= currentpageindex;
        }

    }


    
/// <summary>
    
/// DataNavigtor鼠标点击事件委托声明
    
/// </summary>

    public delegate void ClickEventHandler(object sender, DataNavigatorClickEventArgs e);

    
/// <summary>
    
/// DataNavigator 的摘要说明。
    
/// </summary>

    [DefaultProperty("Text"), 
        ToolboxData(
"<{0}:DataNavigator runat=server></{0}:DataNavigator>")]
    
public class DataGridNavigator : System.Web.UI.WebControls.WebControl,IPostBackEventHandler
    
{
//        private string text;
//        private int _pagecount, _currentpageindex, _totalrecord, _pagesize;
        private System.Web.UI.WebControls.DataGrid _datagrid;
        
private DataSet _dataset;

        
/// <summary>
        
/// 当“首页”链接被点击时,事件产生。.
        
/// </summary>

        //[ResDescription("FirstPageClick")]
        public event ClickEventHandler FirstPageClick;
        
        
/// <summary>
        
/// 当“上一页”链接被点击时,事件发生。
        
/// </summary>

        //[ResDescription("PreviousPageClick")]
        public event ClickEventHandler PreviousPageClick;
        
        
/// <summary>
        
/// 当“下一页”链接被点击时,事件发生。
        
/// </summary>

        //[ResDescription("NextPageClick")]
        public event ClickEventHandler NextPageClick;
        
        
/// <summary>
        
/// 当“末页”链接被点击时,事件发生。
        
/// </summary>

        //[ResDescription("LastPageClick")]
        public event ClickEventHandler LastPageClick;
        
        
/// <summary>
        
/// 当“Go”链接被点击时,事件发生。
        
/// </summary>

        //[ResDescription("GotoPageClick")]
        public event ClickEventHandler GotoPageClick;
        
        
/// <summary>
        
/// 显示错误信息对话框。
        
/// </summary>
        
/// <param name="Message">错误信息</param>

        private void ShowMessage(string Message)
        
{
            
if(!this.Page.IsStartupScriptRegistered("DataNavigatorError"))
            
{
                StringBuilder sb 
= new StringBuilder();
            
                sb.Append(
"<script language=javascript>\n");
                sb.Append(
"window.alert('" + Message + "');\n");
                sb.Append(
"</script>");
            
                
this.Page.RegisterStartupScript("DataNavigatorError",sb.ToString());
            }

        }


        
/// <summary>
        
/// 当点击“首页”链接时被访问。
        
/// </summary>
        
/// <param name="e">事件参数</param>

        protected virtual void OnFirstPageClick(DataNavigatorClickEventArgs e)
        
{
            
if (FirstPageClick != null)
                FirstPageClick(
this, e);
            CurrentPageIndex 
= 0;
            DataGridDataBind();
        }

        
        
/// <summary>
        
/// 当点击“上一页”链接时被访问。
        
/// </summary>
        
/// <param name="e">事件参数</param>

        protected virtual void OnPreviousPageClick(DataNavigatorClickEventArgs e)
        
{
            
if (PreviousPageClick != null)
                PreviousPageClick(
this, e);
            CurrentPageIndex 
--;
            DataGridDataBind();
        }


        
/// <summary>
        
/// 当点击“下一页”链接时被访问
        
/// </summary>
        
/// <param name="e">事件参数</param>

        protected virtual void OnNextPageClick(DataNavigatorClickEventArgs e)
        
{
            
if (NextPageClick != null)
                NextPageClick(
this, e);
            CurrentPageIndex 
++;
            DataGridDataBind();
        }

        
        
/// <summary>
        
/// 当点击“末页”链接时被访问
        
/// </summary>
        
/// <param name="e">事件参数</param>

        protected virtual void OnLastPageClick(DataNavigatorClickEventArgs e)
        
{
            
if (LastPageClick != null)
                LastPageClick(
this, e);
            CurrentPageIndex 
= PageCount - 1;
            DataGridDataBind();
        }

        
        
/// <summary>
        
/// 当点击“Go”链接时被访问
        
/// </summary>
        
/// <param name="e">事件参数</param>

        protected virtual void OnGotoPageClick(DataNavigatorClickEventArgs e)
        
{
            
if (GotoPageClick != null)
                GotoPageClick(
this,e);

            
if ((e.CurrentPageIndex >= 0 ) & (e.CurrentPageIndex < PageCount))
            
{
                CurrentPageIndex 
= e.CurrentPageIndex;
                DataGridDataBind();
            }

        }


        
/// <summary>
        
/// 
        
/// Enables a server control to process an event raised when a form is posted to server.
        
/// </summary>
        
/// <param name="eventArgument"></param>

        public virtual void RaisePostBackEvent(string eventArgument)
        
{
            
int temp;
            
switch(eventArgument)
            
{
                    
// 点击的“首页”链接
                case"FirstPage":
                    
this.OnFirstPageClick(new DataControl.DataNavigatorClickEventArgs(0));
                    
break;
                    
// 点击的“上一页”链接
                case "PreviousPage":
                    
this.OnPreviousPageClick(new DataControl.DataNavigatorClickEventArgs(CurrentPageIndex));
                    
break;
                    
// 点击的“下一页”链接
                case "NextPage":
                    
this.OnNextPageClick(new DataControl.DataNavigatorClickEventArgs(CurrentPageIndex));
                    
break;
                    
// 点击的“末页”链接
                case "LastPage":
                    
this.OnLastPageClick(new DataControl.DataNavigatorClickEventArgs(PageCount));
                    
break;
                    
// 点击的“Go”链接
                default:
                    
try
                    
{
                        temp 
= System.Convert.ToInt32(eventArgument);    
                        
if ((temp < 1| (temp > PageCount))
                        
{
                            
this.ShowMessage("页数超出范围!");
                            
return;
                        }

                        
else
                        
{
                            temp 
--;
                            CurrentPageIndex 
= temp;
                            OnGotoPageClick(
new DataControl.DataNavigatorClickEventArgs(CurrentPageIndex));
                        }

                    }

                    
catch(Exception)
                    
{
                        
this.ShowMessage("请输入数字!");
                    
                    }
                    
                    
break;
            }

        }


        [Bindable(
false),
        Category(
"Data"),
        DefaultValue(
"")]
        
public System.Web.UI.WebControls.DataGrid DataGrid
        
{
            
get 
            

                
return (DataGrid)_datagrid;
//                return (DataGrid)this.ViewState["NavigatorDataGrid"];
            }

            
set 
            
{            
                
try
                
{
                    _datagrid 
= value; 
//                    this.ViewState["NavigatorDataGrid"] = value;
                    CurrentPageIndex = _datagrid.CurrentPageIndex;
                    PageCount 
= _datagrid.PageCount;
                    PageSize 
= _datagrid.PageSize;
                    CurrentPageSize 
= _datagrid.Items.Count;
                    
if (DataGrid.DataSource!=null)
                    
{
                        TotalRecord 
= 0;
                        DataSet dataset 
= (DataSet)DataGrid.DataSource;
                        
if (DataGrid.DataMember != string.Empty)
                            TotalRecord 
= dataset.Tables[DataGrid.DataMember].Rows.Count;
                        
else
                            TotalRecord 
= dataset.Tables[0].Rows.Count;
                    }
    
                }

                
catch(Exception ee)
                
{
                    ShowMessage(ee.Message);
                }

            }

        }

        
        [Bindable(
false),
        Category(
"Data"),
        DefaultValue(
"")]
        
public System.Data.DataSet DataGridDataSet
        
{
            
get
            
{
                
return (DataSet)_dataset;
            }

            
set 
            
{
                
if (value != null)
                
{
                
                    _dataset 
= value; 
                    
if(((DataGrid)_datagrid).DataMember != string.Empty)
                        TotalRecord 
= _dataset.Tables[DataGrid.DataMember].Rows.Count;
                    
else
                        TotalRecord 
= _dataset.Tables[0].Rows.Count;                    
    
                }

            }

        }

        [Bindable(
false),
        Category(
"Appearance"),
        DefaultValue(
"")]
        
public int CurrentPageIndex
        
{
            
get
            
{
                
return (int)ViewState["CurrentPageIndex"]; 
            }

            
set 
            
{
                ViewState[
"CurrentPageIndex"= value;
            }

        }


        [Bindable(
false),
        Category(
"Appearance"),
        DefaultValue(
"")]
        
public int PageCount
        
{
            
get
            
{
                
return (int)ViewState["PageCount"]; 
            }

            
set 
            
{
                ViewState[
"PageCount"= value;
            }

        }


        [Bindable(
false),
        Category(
"Appearance"),
        DefaultValue(
"")]
        
public int TotalRecord
        
{
            
get
            
{
                
return (int)ViewState["TotalRecord"];
            }

            
set 
            
{
                ViewState[
"TotalRecord"= value;
            }

        }


        [Bindable(
false),
        Category(
"Appearance"),
        DefaultValue(
"")]
        
public int PageSize
        
{
            
get
            
{
                
return (int)ViewState["PageSize"]; 
            }

            
set 
            
{
                ViewState[
"PageSize"= value;
            }

        }


        
public int CurrentPageSize
        
{
            
get 
            
{
                
return (int)ViewState["CurrentPageSize"];
            }

            
set
            
{
                ViewState[
"CurrentPageSize"= value;
            }

        }



        [Bindable(
true), 
            Category(
"Appearance"), 
            DefaultValue(
"")] 
        
public string Text 
        
{
            
get
            
{
                
return (string)ViewState["Text"];
            }

 
            
set
            
{
                ViewState[
"Text"= value;
            }

        }


        
public void DataGridDataBind()
        
{
            
if (DataGrid != null)            
            
{
                DataGrid.EditItemIndex 
= -1;
                DataGrid.CurrentPageIndex 
= CurrentPageIndex;
                
if (DataGrid.DataMember == string.Empty)
                    DataGrid.DataMember 
= this.DataGridDataSet.Tables[0].TableName;
                DataGrid.DataBind();                
            }

        }

        
/// <summary> 
        
/// 将此控件呈现给指定的输出参数。
        
/// </summary>
        
/// <param name="output"> 要写出到的 HTML 编写器 </param>

        protected override void Render(HtmlTextWriter output)
        
{
            
string script = "<script type=""text/javascript"">";
            script 
+= "function gettext(){ return document.all['GoToTextBox'].value;}";
            script 
+= "</script>"
            
            output.Write(script);
            output.Write(
"共有"+TotalRecord.ToString()+"记录");
            output.Write(
"&nbsp;");
            
if (CurrentPageIndex == 0)
            
{
                output.Write(
"上一页&nbsp;下一页&nbsp;");
                output.Write(
"&nbsp;");
            }

            
else
            
{
                output.Write(
"<a  href=javascript:"+this.Page.GetPostBackClientEvent(this,"FirstPage")+">上一页</a>");
                output.Write(
"&nbsp;");
                output.Write(
"<a  href=javascript:"+this.Page.GetPostBackClientEvent(this,"PreviousPage")+">下一页</a>");
                output.Write(
"&nbsp;");
            }

            
            
if (CurrentPageIndex < PageCount-1)
            
{
                output.Write(
"<a class='NextPgBtn' href=javascript:"+this.Page.GetPostBackClientEvent(this,"NextPage")+"></a>");
                output.Write(
"&nbsp;");
                output.Write(
"<a class='LastPgBtn' href=javascript:"+this.Page.GetPostBackClientEvent(this,"LastPage")+"></a>");
                output.Write(
"&nbsp;");
            }

            
else
                output.Write(
"下一页&nbsp;末页&nbsp;");

            output.Write(
"&nbsp;&nbsp;转到&nbsp;");
            output.Write(
"<input");
            
if (PageCount == 1)
                output.Write(
" disabled = 'disabled' ");
            output.Write(
" Type=Text Name=GoToTextBox ");
            
int temp = CurrentPageIndex + 1;
            output.Write(
" value = "+temp.ToString());
            output.Write(
" size = 2>");
            output.Write(
"/"+PageCount.ToString()+"");
            output.Write(
"&nbsp;&nbsp;");
            output.Write(
"<input Type=Button Name=GoToButton  value = 定位 onclick = javascript:__doPostBack('"+this.ID+"',gettext())>");
            output.Write(
"&nbsp;&nbsp;");
            output.Write(
"每页"+CurrentPageSize.ToString()+"/"+PageSize.ToString()+"条记录");
        }


        
public  DataGridNavigator()
        
{
            CurrentPageIndex 
= 0;
            PageCount 
= 0;
            TotalRecord 
= 0;
            PageSize 
= 0;
            CurrentPageSize 
= 0;
            Text 
= "";
        }

    }

}

但是,每页刷新的时候,DataGrid和DataSet需要重新付值,这个问题写个版本在解决吧,呵呵,这个我做的第一个DotNet控件!

posted on 2005-08-15 09:25  junetan  阅读(1563)  评论(0编辑  收藏  举报

导航