原创 web自定义控件示例,一个自动分页的datagrid例子收藏

新一篇: 打造自己的asp.net验证控件 | 旧一篇: Repeater的应用之嵌套和行操作

 

以下是一个web自定义的示例,几年前写的,写得也不是很好,权当抛砖引玉。

主要实现datagrid的分页功能:

调用使用方法:

        this.DataGrid1.ConnectionString =  DataClass.ConnectionString; //这里指定一个连接字串。
        this.DataGrid1.strSQL = strSQL;
        this.DataGrid1.DataBind();

如果当前的sql中有identitykey,则必须指定

     this.DataGrid1.IdentityKey = "字段";

然后绑定。

  如果是acesss数据库

    this.DataGrid1.bIsAccess  = true;

还有几个其他的可选参数,看看代码就明白了。

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Data.OleDb;

namespace ZFControls
   
{
    
/// <summary>
    
/// DataGrid
    
/// 实现功能
    
/// </summary>

    [  
    ToolboxData(
"<{0}:DataGrid runat=server></{0}:DataGrid>"),
    ToolboxBitmap(
typeof(ZFControls.DataGrid),  "Grid.bmp")
     
    ]
    
public class DataGrid : System.Web.UI.WebControls.DataGrid,IPostBackEventHandler
    
{
         
         
        
public DataGrid()
        
{
            
if(this.Context == null)
            
{
                
this.ShowFooter = true;//显示设计视图
            }

        }


         

        
private int CurrentPageNo 
        
{
            
get
            
{
                 
                
if(ViewState["PageNo"]==null)
                
{
                    ViewState[
"PageNo"= 1 ;
                }

                
return (int)ViewState["PageNo"];
            }

            
set
            
{
                ViewState[
"PageNo"=value;
            }

        }


        
/// <summary>
        
/// 是否在列表中显示记录数,默认为True
        
/// </summary>

        private bool  ShowRecordCount
        
{
            
get
            
{
                 
                
if(ViewState["ShowRecordCount"]==null)
                
{
                    ViewState[
"ShowRecordCount"= true ;
                }

                
return (bool)ViewState["ShowRecordCount"];
            }

            
set
            
{
                ViewState[
"ShowRecordCount"=value;
            }

        }


        
/// <summary>
        
/// 是否使用默认的样式 默认为true
        
/// </summary>

        public bool  bDefaultStyle
        
{
            
get
            
{
                 
                
if(ViewState["DefaultStyle"]==null)
                
{
                    ViewState[
"DefaultStyle"= true ;
                }

                
return (bool)ViewState["DefaultStyle"];
            }

            
set
            
{
                ViewState[
"DefaultStyle"=value;
            }

        }


        
public string SortExpression 
        
{
            
get
            
{
                
if(ViewState["SortExpression"]==null)
                
{
                    ViewState[
"SortExpression"= "" ;
                }

                
return (string)ViewState["SortExpression"];
            }

            
set
            
{
                ViewState[
"SortExpression"=value;
            }

        }


        
public bool bIsAccess 
        
{
            
get
            
{
                
if(ViewState["IsAccess"]==null)
                
{
                    ViewState[
"IsAccess"=  false ;
                }

                
return (bool)ViewState["IsAccess"];
            }

            
set
            
{
                ViewState[
"IsAccess"=value;
            }

        }


        
private int RecordCount 
        
{
            
get
            
{
                
if(ViewState["RecordCount"]==null)
                
{
                    ViewState[
"RecordCount"= 0;
                }

                
return (int)ViewState["RecordCount"];
            }

            
set
            
{
                ViewState[
"RecordCount"=value;
            }

        }

        
public string IdentityKey 
        
{
            
get
            
{
                
if(ViewState["IdentityKey"]==null)
                
{
                    ViewState[
"IdentityKey"= "";
                }

                
return (string)ViewState["IdentityKey"];
            }

            
set
            
{
                ViewState[
"IdentityKey"=value;
            }

        }

        
private int  PageCount1
        
{
            
get
            
{
                
if(this.Context == nullreturn 0;

                
if(this.RecordCount % this.PageSize ==0)
                    
return this.RecordCount /this.PageSize;
                
else
                    
return Convert.ToInt32(this.RecordCount /this.PageSize) +1;
            }

         
             
        }



        
private System.Web.UI.WebControls.DataGridItem objFooterItem; //footer Item
    
        
public string strSQL 
        
{
            
get
            
{
                
if(ViewState["strSQL"]==null)
                
{
                    ViewState[
"strSQL"= "" ;
                }

                
return (string)ViewState["strSQL"];
            }

            
set
            
{
                
if((string)value != this.strSQL)
                
{
                    
this.CurrentPageNo = 1;//更改了strSQL,重置参数
                    this.RecordCount = 0;
 
                }

                

                ViewState[
"strSQL"=value;

            }

        }

    
        
public string  ConnectionString
        
{
            
get
            
{
                
if(ViewState["ConnectionString"]==null)
                
{
                    ViewState[
"ConnectionString"= "";
                }

                
return (string)ViewState["ConnectionString"];
            }

            
set
            
{
                ViewState[
"ConnectionString"=value;
            }

        }


        
public string ImagePath 
        
{
            
get
            
{
                
if(ViewState["ImagePath"]==null)
                
{
                    ViewState[
"ImagePath"= "../images/" ;
                }

                
return (string)ViewState["ImagePath"];
            }

            
set
            
{
                ViewState[
"ImagePath"=value;
        
            }

        }


        
private string OldSortExpression