DataGrid自定义分页控件WebControl

该博客为转载内容,转载自https://www.cnblogs.com/kbbb/archive/2004/11/17/64851.html ,原内容可能与JavaScript和UI相关。
用了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
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// OnClick事件参数。
    
/// </summary>

    public class DataNavigatorClickEventArgs : EventArgs
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
int _currentpageindex;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// DataGrid当前页索引号。
        
/// </summary>

        public int CurrentPageIndex
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _currentpageindex; }
        }


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

        public DataNavigatorClickEventArgs(int currentpageindex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            _currentpageindex 
= currentpageindex;
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// DataNavigtor鼠标点击事件委托声明
    
/// </summary>

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

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// DataNavigator 的摘要说明。
    
/// </summary>

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

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

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

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

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

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

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

        private void ShowMessage(string Message)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if(!this.Page.IsStartupScriptRegistered("DataNavigatorError"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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());
            }

        }


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

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

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

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


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

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

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

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

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

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

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

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int temp;
            
switch(eventArgument)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                    
// 点击的“首页”链接
                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
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        temp 
= System.Convert.ToInt32(eventArgument);    
                        
if ((temp < 1| (temp > PageCount))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
this.ShowMessage("页数超出范围!");
                            
return;
                        }

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

                    }

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

        }


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

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

            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{            
                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    _datagrid 
= value; 
//                    this.ViewState["NavigatorDataGrid"] = value;
                    CurrentPageIndex = _datagrid.CurrentPageIndex;
                    PageCount 
= _datagrid.PageCount;
                    PageSize 
= _datagrid.PageSize;
                    CurrentPageSize 
= _datagrid.Items.Count;
                    
if (DataGrid.DataSource!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        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)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    ShowMessage(ee.Message);
                }

            }

        }

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

            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (value != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                
                    _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
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return (int)ViewState["CurrentPageIndex"]; 
            }

            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ViewState[
"CurrentPageIndex"= value;
            }

        }


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

            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ViewState[
"PageCount"= value;
            }

        }


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

            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ViewState[
"TotalRecord"= value;
            }

        }


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

            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ViewState[
"PageSize"= value;
            }

        }


        
public int CurrentPageSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return (int)ViewState["CurrentPageSize"];
            }

            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ViewState[
"CurrentPageSize"= value;
            }

        }



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

 
            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ViewState[
"Text"= value;
            }

        }


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

        }

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

        protected override void Render(HtmlTextWriter output)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
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)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                output.Write(
"上一页&nbsp;下一页&nbsp;");
                output.Write(
"&nbsp;");
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            CurrentPageIndex 
= 0;
            PageCount 
= 0;
            TotalRecord 
= 0;
            PageSize 
= 0;
            CurrentPageSize 
= 0;
            Text 
= "";
        }

    }

}

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

转载于:https://www.cnblogs.com/kbbb/archive/2004/11/17/64851.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值