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

WPF Datagrid 分页控件实现教程 分页是将信息量庞大的数据集分成较小、易于管理的块,每次仅展示其中的一部分给用户。这一概念在计算机科学和信息技术中广泛使用,特别是在用户界面设计和数据库操作中。分页机制的引入极大地提高了用户体验,因为它可以减少单次加载的数据量,加速内容的展示速度,降低内存使用,从而提升应用程序的响应性和性能。ICollection是一个定义在.NET Framework中的接口,它提供了用于访问集合的基本方法和属性。当与CollectionView一起使用时,此接口的以下方法特别重要:Count。 阅读详情

 

以下是一个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 == null) return 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
        
{
            
get
            
{
                
if(ViewState["OldSortExpression"]==null)
                
{
                    ViewState[
"OldSortExpression"] = "";
                }

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

            
set
            
{
                ViewState[
"OldSortExpression"] =value;
            }

        }


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

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

            
set
            
{
                ViewState[
"ShowNoRecordMsg"] =value;
            }

        }


         
        
         
         
   


        
private string GetStyleString()
        
{
            
string res ="";
            
foreach(string sKey in this.Style.Keys)
            
{
                res 
+= sKey +":"+ this.Style[sKey] +";";
            }

            
return res;

            
            
        }


    
         
        
        
protected override void OnSortCommand(DataGridSortCommandEventArgs e)
        
{
            
            
            
string sSort = e.SortExpression.Trim();
            
if(this.SortExpression.Trim().Split(' ')[0] == e.SortExpression)
            
{
                sSort 
= this.SortExpression;
            }


            
string direct ="ASC";
            
if (sSort.IndexOf(" ")>-1)
            
{
                direct 
= sSort.Split(' ')[1];
                
                
                
if(direct.ToUpper() =="ASC")
                
{
                    direct 
="DESC";
                    
                    
                }

                
else
                
{
                    direct 
="ASC";
                    
                }

                
                
            }

            
            
            sSort 
= sSort.Split(' ')[0] +" "+ direct;
            
            
if(this.OldSortExpression!="")
            
{
                DataGridColumn oOldCol 
= this.GetColumnBySortExpression(this.OldSortExpression);
                oOldCol.HeaderText 
=  oOldCol.HeaderText.Split(' ')[0];
            }


            DataGridColumn col 
= GetColumnBySortExpression(e.SortExpression);

            
if(direct =="ASC")
            
{
                col.HeaderText 
= col.HeaderText.Split(' ')[0] +" <font class='gridarrow' face='webdings'>5</font>";
            }

            
else
            
{
                col.HeaderText 
= col.HeaderText.Split(' ')[0] +" <font class='gridarrow'   face='webdings'>6</font>";
            }

            
this.OldSortExpression = e.SortExpression;
            
this.SortExpression = sSort;
            
this.DataBind();
        }


        
private  DataGridColumn GetColumnBySortExpression(string sort)
        
{
            
for (int i=0;i<this.Columns.Count;i++)
            
{
                
if(this.Columns[i].SortExpression ==sort.Split(' ')[0])
                
{
                    
return this.Columns[i];
                }

            }

             
            
return null;
        }

        
    
         

     

        
protected override void OnItemCreated(DataGridItemEventArgs e)
        
{
            
            
 
            
if(e.Item.ItemType == ListItemType.Footer)
            
{
                
                
this.objFooterItem = e.Item;//将它保存下来,目前还不能取得它的Visible 属性,最后再处理
                
                
            }

            
else if(e.Item.ItemType == ListItemType.Item 
                
|| e.Item.ItemType == ListItemType.AlternatingItem)
            
{
                e.Item.Attributes.Add(
"onmouseover","javascript:return DataGridOnMouseOver();");
                e.Item.Attributes.Add(
"onmouseout","javascript:return DataGridOnMouseOut();");

            }

            
            
base.OnItemCreated (e);
        }




         
     
        
        
private string GetPagerText ()
        
{
             
            
string res = @"<!--{3}-->
            <table align='right'>
            <TR>
            <TD>
                <span  {0} {9} title='回到首页'  onclick=""if(this.disabled) return ;hidAction_{2}.value='1';btnPager_{2}.click();"" style='CURSOR: hand; ;border: solid 1px #ffffff;padding-right:2px;'><font face='webdings'>7</font>[首页]</span>
            </TD>
            <TD>
            <span     {0} {9} title='回到上一页' onclick=""if(this.disabled) return ;hidAction_{2}.value='2';btnPager_{2}.click();"" style='CURSOR: hand; border: solid 1px #ffffff;padding-right:2px;'> <font face='webdings'>3</font>上页</span>
            </TD>
            <TD>
            <span    {1} {9} title='回到下一页' onclick=""if(this.disabled) return ;hidAction_{2}.value='3';btnPager_{2}.click();"" style='CURSOR: hand;border: solid 1px #ffffff;padding-right:2px;'> 下页<font face='webdings'>4</font></span>
            </TD>            
            <TD>
            <span   {1} {9} title='回到最后一页' onclick=""if(this.disabled) return ;hidAction_{2}.value='4';btnPager_{2}.click();"" style='CURSOR: hand; border: solid 1px #ffffff;padding-right:2px;'>[末页]<font face='webdings'>8</font></span> 
            </TD>
            <TD>    
                <span id='lblCurrentIndex' style='CURSOR: hand' >[{4}/{8}页]</span> 
            </TD><TD>
            <span id='tbl1' style='CURSOR: hand;height:20px;border: solid 0px #e0e0e0;padding:2px;'></TD><TD>{10}</TD><TD>跳到</TD><TD></span><input name='txtGoPage_{2}' value='{7}' type='text' id='txtGoPage' class='textbox1' style='width:20px;height:18px' />
            </TD><TD>    <INPUT class='btnPager' onclick=""hidAction_{2}.value='5';btnPager_{2}.click();""     type=button value=' GO '> </TD><TD>
                <span id='tbl2' style='CURSOR: hand;height:20px;border: solid 0px #e0e0e0;padding:2px;'>每页显示</span></TD><TD><input name='txtRowsPager_{2}' type='text' id='txtRowsPager' value='{5}' class='textbox1' style='width:20px;height:18px' /></TD><TD> <INPUT class='btnPager' onclick=""hidAction_{2}.value='6';btnPager_{2}.click();""    type=button value=重置> 
            </TD><TD><input name='hidAction_{2}' id='hidAction_{2}'  type='hidden' />
";
            
if(this.Context != null)//非设计视图
            {
                    res 
+= @"
            <input type='button' name='btnPager_{2}'  id='btnPager_{2}'   {6} value='Button' id='btnPager_{2}' style='DISPLAY: none' /> 
";
            }

            res 
+=@"</TD><TD>
            </TR></TABLE>
";
            
            
             
            
            
//System.Web.HttpContext.Current.Response.Write(System.Web.HttpContext.Current.Server.HtmlEncode(res)); 
            
//System.Web.HttpContext.Current.Response.Flush();
            string sP0  =   this.CurrentPageNo > 1 ?"":"disabled";
            
string sP1  =   this.CurrentPageNo < this.PageCount1 ?"":"disabled";
            
string sP2    =    this.ID;
            
string sP3    =     this.ImagePath ;
            
string sP4    =    "<font color='red'>"+this.CurrentPageNo.ToString()+"</font>";
            
string sP5    =    this.PageSize.ToString();
            
string sP6    =    "onclick="javascript:"+this.Page.GetPostBackEventReference(this,"btnPager_"+sP2)+""";
            
string sP7    =    this.CurrentPageNo.ToString();
            
//System.Web.HttpContext.Current.Response.Write(this.PageCount1+"**");

            
string sP8    =    "<font color='red'>"+this.PageCount1.ToString()+"</font>";
            
string sP9    =    "onmouseover='javascript:{0}_PagerOnMouseOver(this);' onmouseout='javascript:{0}_PagerOnMouseOut(this);'  onmousedown='javascript:{0}_PagerOnMouseDown(this);' onmouseup='javascript:{0}_PagerOnMouseUp(this);'";
            
string sP10  = "";
            
if(this.ShowRecordCount) //显示记录数
            {
                sP10  
= "[<font color='red'>"
                    
+ (((this.CurrentPageNo-1)* this.PageSize)+1).ToString()+"</font>-<font color='red'>"
                    
+(  this.CurrentPageNo*this.PageSize <this.RecordCount?this.CurrentPageNo*this.PageSize:this.RecordCount ).ToString()+"</font>/<font color='red'>"+this.RecordCount.ToString()+"</font>条] ";            
            }

            sP9            
=    String.Format(sP9,sP2);
            res 
= String.Format(res,sP0,sP1,sP2,sP3,sP4,sP5,sP6,sP7,sP8,sP9,sP10);

            
            
string res1 = @"
        <SCRIPT LANGUAGE='javascript'>
        <!--
            function {0}_PagerOnMouseOver(obj)
            {
                if(obj.disabled) return;obj.runtimeStyle.cssText ='border-right:solid 1px gray;border-bottom:solid 1px gray ';
            }
            function {0}_PagerOnMouseOut(obj)
            {
                if(obj.disabled) return;obj.runtimeStyle.cssText = '';
            }
            function {0}_PagerOnMouseDown(obj)
            {
                if(obj.disabled) return;
                obj.runtimeStyle.cssText ='border-bottom:solid 1px white;border-right:solid 1px white;border-top:solid 1px gray;border-left:solid 1px gray;';
            }
            function {0}_PagerOnMouseUp(obj)
            {
                if(obj.disabled) return;obj.runtimeStyle.cssText = 'border-top:solid 1px white;border-left:solid 1px white;border-bottom:solid 1px gray;border-right:solid 1px gray;';
            }
        //-->
        </SCRIPT>
        <!--********************************************--->
        
";
            
//res1 = String.Format(res1,sP2);
            res1 = res1.Replace("{0}",sP2);
            
return res+res1;
        }

        

        
protected override void OnPreRender(EventArgs e)
        
{
            
//base.OnPreRender(e);
            
//return;
            if(bDefaultStyle)//默认的样式
            {
                
this.BorderColor = (Color)new System.Drawing.ColorConverter().ConvertFromString("#E3EDF5");
                
this.Attributes["Class"] = "GridTable";
            }
//处理FoooterItem
            
//找到第一个Visible = True的列
            if(this.objFooterItem !=    null)
            
{
                
int i = 0;
                
for (i = 0;i<  this.Columns.Count;i++)
                
{
                    
if(this.Columns[i].Visible )
                    
{
                        
break;
                    }

                }



                
while(this.objFooterItem.Cells.Count>i+1)
                
{
                    objFooterItem.Cells.RemoveAt(
0);
                }

                objFooterItem.Cells[i].ColumnSpan 
= this.Columns.Count-i;
                
if(this.bDefaultStyle)
                
{
                    objFooterItem.Cells[i].Attributes[
"class"] = "t1";
                }

                
if(this.Items.Count ==0 && this.ShowNoRecordMsg) //没有记录
                {
                    
this.objFooterItem.Cells[i].Text =@"<table width='100%' cellspacing='0' cellpadding='0'><TR><TD height='20px' style='color:gray' align='center'>信息:没有查询到任何记录!</td></tr>    
                                             </table>
";
                }
    
                
else
                
{
                    
this.objFooterItem.Cells[i].HorizontalAlign = HorizontalAlign.Right;
                    
this.objFooterItem.Cells[i].Text = this.GetPagerText();
                    
this.objFooterItem.Cells[i].Height = 22;
                    
//e.Item.Cells[0].Style.Add("border-top","solid 2px #336699");
                }

            }





            
if(this.HeaderStyle.CssClass == "")
            
{
                
this.HeaderStyle.CssClass = "gridheader";
            }

            
if(this.ItemStyle.CssClass =="")
            
{
                
this.ItemStyle.CssClass ="t1";
            }

            
if(this.AlternatingItemStyle.CssClass =="")
            
{
                
this.AlternatingItemStyle.CssClass ="t2";
            }

     
            
this.ShowFooter = true;
            
base.OnPreRender (e);
        }


        

        
         
      


        

        
        
        
        
        
        
        
        [Bindable(
true), 
        Category(
"Appearance"), 
        DefaultValue(
"")] 
        
/// <summary> 
        
/// 将此控件呈现给指定的输出参数。
        
/// </summary>
        
/// <param name="output"> 要写出到的 HTML 编写器 </param>

        
         
protected override void Render(HtmlTextWriter output)
        
{
            
            
if(this.Context == null) //设计
            {
                output.Write(
"<div style='width:100%;border:solid 1px #336699'>");
            
                output.Write(
"<font color='orange'>请注意:<BR>1、必须指定的参数:ConnectString,strSQL<BR> 2、如果查询中只有一张表且有IdentityKey必须指定该Key</font>");
            }

            
base.Render(output);
 
            
if(this.Context == null)
            
{
                output.Write(
this.GetPagerText());
                output.Write(
"<DIV>");
            }

            
            
        }

         
        
/// <summary>
        
/// 利用存储过程进行分页
        
/// </summary>
        
/// <param name="strSQl">sql</param>
        
/// <param name="PrimaryKey">关键字段,一般为表的主健</param>
        
/// <param name="PageNo">当前页从1开始</param>
        
/// <param name="PageSize">页面大小</param>
        
/// <param name="SortExpression">排序表达式</param>
        
/// <param name="RecordCount">记录总数</param>
        
/// <returns></returns>

        public  DataSet GetSqlResult(string strSQL,string PrimaryKey,int PageNo,int PageSize,string SortExpression,ref int RecordCount)
        
{
            SqlConnection conn 
=null; 
            SqlCommand cmd 
=null;
            SqlDataAdapter dapt 
=null;
            
try
            
{
                conn
= new  SqlConnection(this.ConnectionString);
                
                cmd 
= new SqlCommand("GetPageResult",conn);
                cmd.CommandTimeout 
= 60000;
                cmd.CommandType 
= CommandType.StoredProcedure;
                SqlParameter pSql 
= cmd.Parameters.Add("@sql",SqlDbType.NVarChar,4000);
                pSql.Value 
= strSQL;
                SqlParameter pPKey 
= cmd.Parameters.Add("@PKey",SqlDbType.VarChar,50);
                pPKey.Value 
= PrimaryKey;
                SqlParameter pPageNo 
= cmd.Parameters.Add("@PageNo",SqlDbType.Int,4);
                pPageNo.Value 
= PageNo;
                SqlParameter pPageSize 
= cmd.Parameters.Add("@PageSize",SqlDbType.Int,4);
                pPageSize.Value 
= PageSize;
                SqlParameter pSort 
= cmd.Parameters.Add("@sort",SqlDbType.VarChar,50);
                pSort.Value 
= SortExpression;
                SqlParameter pRecordCount 
= cmd.Parameters.Add("@RecordCount",SqlDbType.Int,4);
                
//pRecordCount.Value = SortExpression;
                pRecordCount.Direction = ParameterDirection.Output;
                dapt 
= new SqlDataAdapter(cmd);
                conn.Open();
                DataSet ds 
= new DataSet();
                dapt.Fill(ds,
"Table1");
                RecordCount 
=(int)pRecordCount.Value;
                
return ds;
                    
            }

            
catch (Exception e)
            
{
                
throw(e);
                
//return null;
            }

            
finally
            
{
                
if(conn!=null)
                    conn.Dispose();
                
if(cmd!=null)
                    cmd.Dispose();
                
if(dapt!=null)
                    dapt.Dispose();
            }

            
        }


        
/// <summary>
        
/// 利用存储过程进行分页
        
/// </summary>
        
/// <param name="strSQl">sql</param>
        
/// <param name="PrimaryKey">关键字段,一般为表的主健</param>
        
/// <param name="PageNo">当前页从1开始</param>
        
/// <param name="PageSize">页面大小</param>
        
/// <param name="SortExpression">排序表达式</param>
        
/// <param name="RecordCount">记录总数</param>
        
/// <returns></returns>

        public  DataSet GetAccessResult(string strSQL,string PrimaryKey,int PageNo,int PageSize,string SortExpression,ref int RecordCount)
        
{
            
            
            DataSet ds  
= new DataSet();
            
if(System.Web.HttpContext.Current.Session[this.Page.ToString()]==null||(!this.Page.IsPostBack))
            
{

                OleDbConnection conn 
=null; 
                OleDbDataAdapter dapt 
=null;
                
try
                
{
                
                 
                    conn 
= new OleDbConnection(this.ConnectionString);
                    conn.Open();
                    dapt 
= new OleDbDataAdapter(strSQL,conn);
                    
//DataSet ds = new DataSet();
                    dapt.Fill(ds,"Table1");
                    System.Web.HttpContext.Current.Session[
this.Page.ToString()] =ds;
                }

                
catch (Exception e)
                
{
                    
throw(e);
                    
//return null;
                }

                
finally
                
{
                    
if(conn!=null)
                        conn.Dispose();
                     
                    
if(dapt!=null)
                        dapt.Dispose();
                }

            }

            
else
            
{
                ds 
=(DataSet)System.Web.HttpContext.Current.Session[this.Page.ToString()];
            }


            DataView dv 
=ds.Tables[0].DefaultView;
            
if(SortExpression!="")
                dv.Sort  
= SortExpression ;
            RecordCount  
= dv.Count;
            
            DataTable dt 
=  ds.Tables[0].Clone();
            
int iStart = (PageNo-1) * PageSize+1;
            
int iEnd =    PageNo * PageSize;
            
//System.Web.HttpContext.Current.Response.Write(this.PageCount1);
            
            
if(iEnd>dv.Count)
                iEnd 
= dv.Count;

            
if(iStart>0 && iEnd>=iStart)
            
{
                
for(int i = iStart-1;i<iEnd;i++)
                
{
                     DataRow row 
= dt.NewRow();
                    row.ItemArray  
= dv[i].Row.ItemArray;
                
                    dt.Rows.Add(row);
                }

            }

            
//ds = null;
            dv = null;
            ds 
= new DataSet();
            ds.Tables.Add(dt);
            
return ds;

        }


        
        
private string GetRequestValue(string sKey)
        
{
            
object o = this.Page.Request.Form[sKey];
            
if(o!=null)
            
{
                
return o.ToString().Trim();
            }

            
return "";
        }



        
        
public override void DataBind()
        
{    
            
if(this.Context  == null)
            
{
                
base.DataBind();
                
return;
            }

            
if(this.ConnectionString =="")
            
{
                
throw(new Exception("没有指定ConnectionString"));
            }

            
if(this.strSQL =="")
            
{
                
throw(new Exception("没有指定strSQL"));
            }

            
            
int iCount =0;
            
//Add BY zhaofeng 2004-11-19
            if(this.CurrentPageNo >this.PageCount1)
            
{
                
this.CurrentPageNo = this.PageCount1;
            }

            
if(this.CurrentPageNo == 0 )
                
this.CurrentPageNo = 1;
            
//Add End
            DataSet ds = null;
            
if(this.bIsAccess)
                ds 
= this.GetAccessResult(this.strSQL,this.IdentityKey,this.CurrentPageNo,this.PageSize,this.SortExpression,ref iCount);
            
else

                ds 
= this.GetSqlResult(this.strSQL,this.IdentityKey,this.CurrentPageNo,this.PageSize,this.SortExpression,ref iCount);
            
            
this.RecordCount = iCount;
            
this.DataSource =ds.Tables[0].DefaultView;
            
base.DataBind ();
        }


        
private void DoPager()
        
{
            
string sAcionType = this.GetRequestValue("hidAction_"+this.ID);
            
switch (sAcionType)
            
{
                
case "1":
                    
this.CurrentPageNo =1;
                    
break;
                
case "2":
                    
if(this.CurrentPageNo >1)
                    
{
                        
this.CurrentPageNo = this.CurrentPageNo -1;
                    }

                    
else
                        
return;
                    
break;
                
case "3":
                    
                    
if(this.CurrentPageNo <this.PageCount1)
                    
{
                        
this.CurrentPageNo = this.CurrentPageNo +1;
                    }

                    
else
                        
return ;
                    
break;
                
case "4":
                    
if(this.CurrentPageNo !=this.PageCount1 )
                    
{
                        
this.CurrentPageNo = this.PageCount1;
                    }

                    
else
                        
return ;
                    
break;
                
case "5": //Goto
                    string sCurPage    = this.GetRequestValue("txtGoPage_"+this.ID);
                    
if(CCConvert.IsInt32(sCurPage))
                    
{
                        
int iCurrentPageNo = Convert.ToInt32(sCurPage);
                        
if(iCurrentPageNo >0 && iCurrentPageNo <=this.PageCount1)
                        
{
                            
this.CurrentPageNo = iCurrentPageNo;
                        }

                    }

                    
else
                        
return ;
                    
break;
                
case "6"://重设显示页数
                    string sPageSize = this.GetRequestValue("txtRowsPager_"+this.ID);
                    
if(CCConvert.IsInt32(sPageSize))
                    
{
                        
int iPage =  Convert.ToInt32(sPageSize);
                        
if(iPage>0)
                            
this.PageSize =iPage;
                        
else
                            
return ;
                    }

                    
else
                        
return;
                    
break;
                
default:
                    
return;

            }


             
            
this.DataBind();
        }

        
IPostBackEventHandler 成员
    }

}

 

用到的存储过程:

 

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO





/*****************************
名称:GetPageResult
功能:得到分页记录集
作者:cpp2017
编写时间:2002-08-17
****************************
*/

CREATE      PROCEDURE GetPageResult 
  
@sql nvarchar(4000) ,--Sql Statment
  @PKey varchar(100),---Primary Key Name
  @PageNo int ,--Current Page No
  @PageSize int, --PageSize
  @Sort varchar(50),--Sort Field
  @RecordCount int output --RecordCount 传出参数
  AS 
BEGIN

   
 
DECLARE @sqlStr NVARCHAR(4000);
 
---得到记录总数Start 
if @RecordCount = -1 or @RecordCount is null
begin 
    
SET @sqlStr ='select @count = Count(1) from ('+@sql+') as AA';
    
EXECUTE sp_executesql @sqlStr,N'@count int out',@RecordCount out;
end 
 
---得到记录总数End
 --加上排序 Start 
 IF  @Sort IS not null and @sort <> ''
      
BEGIN
        
        
Set @sort =' order by '+ @sort;
    
END 
--加上排序 End
 IF  (@PageNo =1 ) --第一页
    SET  @sqlStr ='select top ' + cast(@PageSize as varchar(5)) +' * FROM ('+ @sql +' ) AS AA ' + @sort 
 
ELSE
  
  
BEGIN
    
declare @sMaxCount  varchar(10)
    
declare @sMinCount  varchar(10)
    
set     @sMaxCount    = cast(@PageSize*@PageNo as varchar(5))
    
set     @sMinCount    = Convert(nvarchar(10),(@PageNo-1)*@PageSize)
    
      
    
if @PKey != '' or @PKey is null  --如果有主键,注此key必须是identity key
        begin
            
SET  @sqlStr='select top ' + @sMaxCount + ' ' + @PKey+  '  into #temp from ('+ @sql +') as AA ' + @sort +';'
            
Set  @sqlStr = @sqlStr + 'delete from #temp where ' +@PKey + ' in (select top '+@sMinCount +' '+ @PKey +' from #temp);'
            
SET  @sqlStr = @sqlStr +' select A.* from ('+@sql+') AS A INNER JOIN   #temp as B ON A.' +@PKey +'=B.'+@Pkey+' ;drop table #temp'
        
end
    
else
    
Begin    
        
SET  @sqlStr='select top ' + @sMaxCount + ' *  into #temp from ('+ @sql +') as AA ' + @sort +';'
        
SET  @sqlStr =  @sqlStr + 'exec(''alter table #temp add PrimaryKey int identity(1,1);'');  delete from #temp where PrimaryKey in (select top ' +@sMinCount +' PrimaryKey From #temp) '
         
        
SET  @sqlStr =  @sqlStr +';select  * from #temp;drop table #temp' 
    
end 
  
END
   
  
EXECUTE (@sqlStr)
    
print @sqlstr
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

 

 

javascript实现datagrid javascript实现datagrid,支持事件以及参数传递 立即下载

相关推荐

保姆级教程:手把手教你用Python逆向分析极验4.0滑块验证码的w参数生成逻辑

本文提供了一份详细的Python逆向工程教程,手把手教你分析极验4.0滑块验证码的w参数生成逻辑。通过HTTP请求分析、JavaScript逆向和加密算法复现,帮助开发者深入理解验证码防护机制,并实现高效的自动化解决方案。教程涵盖AST反混淆、轨迹生成算法和AES加密等核心技术,适合中高级开发者提升逆向工程能力。

weixin_30172941的博客 304

DataGrid控件属性全解

 DataGrid控件DataGrid控件在一个表格中显示一个数据源的字段 属性说明AllowCustomPaging获取或设置指示是否启用自定义分页的值。AllowPaging获取或设置指示是否启用分页的值。AllowSorting获取或设置指示是否启用排序的值。AlternatingItemStyle获取

星瞳 5239

避坑指南:sherpa-onnx中文语音识别模型实测对比(2024最新版)

本文实测对比了sherpa-onnx框架下Paraformer、Zipformer、SenseVoice等主流中文语音识别模型。针对不同应用场景,从准确率、延迟、资源占用等维度提供选型建议,并分享部署避坑指南与最佳实践,帮助开发者高效选择本地语音转文字方案。

xray4的博客 2291

DataGridView控件中加入下拉框的编程实现

以下是在网上看到的网友发的文章“VC++.NET2005中DataGridView控件中加入下拉框的编程实现”用C#作了 修改的后的方法如下: VS2005中新增加了数据浏览控件DataGridView,虽然我们可以通过其DataGridViewComboBoxColumn方法在DataGridView中添加下拉框列,但随之而来得问题是一整列的下拉框,很不美观,并且还要编程为其绑定数据

ShadowSun的专栏 4418

Dify+MCP Server打造禅道AI智能助手

Dify AI与MCP Server结合打造的禅道AI智能助手,实现了项目管理智能化升级。该方案通过大语言模型和自动化工作流,提供四大核心功能:1)自动化报表生成,支持多维度数据统计与可视化;2)禅道系统管理操作,简化产品/项目/任务管理流程;3)基于知识库的智能问答系统;4)数据库灵活查询与可视化输出。系统采用分层架构,通过MCP协议实现AI与数据源解耦,解决了传统禅道在数据孤岛、工具碎片化等问题。实际应用验证了其在用户管理、任务创建、Bug处理等场景的高效性,显著提升了项目管理效率。

oNew_Lifeo的博客 1710

【实战指南】Green Hills MULTI-IDE 从零安装到工程创建全流程

本文详细介绍了Green Hills MULTI-IDE从零安装到工程创建的全流程,包括系统要求检查、安装步骤、工程配置及调试技巧。特别适合嵌入式开发新手快速上手这款专业IDE工具,提升开发效率。

weixin_28740299的博客 479

对webdatagrid只取有更改的行

在实际工作中,我们也许会对绑定的datagrid进行编辑,有的情况下,我们只需要得到有变更的行,在后台取不仅效率不高且不方便,所以我在前台来进行操作。 具体的步骤为: 1. 在单元格编辑后执行Editing_CellValueChanged事件,记录变更的行号。 2. 模板列(TemplateDataField)中的控件发生改变是不会触发CellValueChanged事件的,所以需要为控件...

weixin_30391339的博客 180

后台扫描工具 - 御剑(珍藏版)附下载

版本说明:御剑后台扫描珍藏版 下载地址:安装包放下方网盘里了 链接:https://pan.baidu.com/s/1Bn7GtWb7AStcjzVahFOjSQ 提取码:zkaq 使用环境:windows 标题工具说明: 御剑后台扫描珍藏版是T00LS大牛的作品,方便查找用户后台登陆地址,同时也为程序开发人员增加了难度,尽量独特的后台目录结构。 附带很强大的字典,字典我们也是可以自己修改的,继续增加规则。 一、御剑后台扫描工具简介: 1、扫描线程自定义:用户可根据自身电脑的配置来设置调节扫描线程 2、

hackzkaq的博客 4万+

DataGrid控件介绍

DataGrid控件介绍 1. 引言 Rich UI中的DataGrid组件可以在表中显示一组行数据。本文将详细介绍DataGrid组件相关信息。 如果想要较好的理解DataGrid,你需要熟悉以下内容: 如何定义表头 如何定义behavior,它是DataGrid组件中包含一系列behavior函数的属性。当组件被渲染时,属性中的behavior函数将被调用。 如何定义lis...

云计算? 787

从零实现深度学习框架【实现自己的PyTorch(已完结)】

本着“凡我不能创造的,我就不能理解”的思想,此系列文章会基于纯Python以及NumPy从零创建自己的深度学习框架,该框架类似PyTorch能实现自动求导。

日积月累,天道酬勤 6862

24、水下传感器网络中混合LEACH协议与遗传算法获取路由节点

本文提出了一种结合遗传算法与LEACH协议的混合路由方案,用于提升水下无线传感器网络(UWSN)的能量利用效率和数据传输稳定性。通过遗传算法优化簇头选择与路径规划,有效避免节点过早死亡,减少能量消耗,在200个节点场景下显著提升网络寿命。相比传统LEACH协议,该方案具有更高的能量利用率、更强的环境适应性,并支持传输历史记录与并行传输序列。实验结果显示,该方案在总能量为2000焦耳时剩余能量达75.6%,远优于LEACH的34.8%能耗表现。适用于海洋监测、石油平台监控及水下灾害预警等复杂环境应用。

i8j9k0l1的博客 112

DataGrid/DataList, 你会用了吗?

DataGrid/DataList, 你会用了吗? By icyer from 优快云  很久以前就想写一些关于DataGrid/DataList的东西,但是一直以来,一方面自感所学未深,另一方面,总觉无从下笔,一拖再拖,离刚开始的念头已距一年有余。DataGrid/DataList在ASP.NET中的重要性,想必就不用我再强调了,凡显示Table类型的数据,大多会使用这两个控件(当然...

weixin_34019144的博客 376

基于 MFC 和 C++ 的语音识别客户端实现方案

语音 SDK:[Microsoft Speech SDK](https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/speech-sdk)(建议使用最新版)1. **Azure 语音服务密钥**:需要在 [Azure 门户](https://portal.azure.com/) 中创建语音资源,获取密钥和区域(如 `eastasia`),替换代码中的占位符。

寒寒豆豆软件公司的博客 156

Henry手记—Web Form中的Datagrid的自定义分页

         Henry手记—Web Form中的Datagrid的自定义分页                                  韩睿  ( 05/31/2003) ASP.NET带给我们很多惊喜,强大的Web Form控件自然是其中的重要部分。这其中,最受关注的当然是Datagrid。在ASP中用HTML标记语法来输出数据的方法在Datagrid数据绑定面前显得如

韩睿(Henry Han)的专栏 2350

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

如何在DataGrid控件上实现分页

1:页面图片显示 2:布局图片显示。DataGrid控件自行布局 我还是附上吧,哈哈,这样比较方便 <tr valign="top"> <td valign="middle" colspan="2"> <table cellspacing="0" cellpadding="0" width="100%" align="center" border="0" style="height:30px"> ...

weixin_48175309的博客 810

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

关于DataGrid自动分页的不能显示的问题

关于DataGrid自动分页的问题在属性生成器中设置DataGrid自动分页后并没有完全结束,这是因为它会在页面中只显示第一页。当点击下一页时页面刷新后竟然还显示第一页。这不是IsPostBack的问题,而是没有写分页代码的问题。DataGrid自动分页后需要写入分页代码。分页代码要改变CurrentPageIndex为e.NewPageIndex。我得一段代码为 private void D

紫颜天龙的专栏 4075

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

DataGrid自定义分页的使用

 DataGrid使用心得(附大量代码)    1. 为 DataGrid 控件设计样式        在之后添加如下代码                                说明:    (1) 在每个标签内主要是  ForeColor   BackColor  Font-Bold 这几个属性值        2. 为 DataGrid 控件添加绑定列            说

张朝玉的专栏 1421

DataGrid 自定义分页导航

DataGrid 自定义分页导航效果:  让DataGrid自己的分页实现这样的效果[1][2][3][4][5][6]  private void grid_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)  {   if(e.Item.ItemType==ListItemT

886

[原创]老生常谈,回顾并整理一下DataGrid分页控件

·摘要素材来自于最近完成的一个.net1.0项目。因为DataGrid自带的分页功能不够完善,所以自己写了个控件,用途是支持DataGrid的自定义分页,主要包含首页、上一页、下一页、尾页;任意跳转页;显示所有页数信息等。这个分页控件可以提供给任何需要的DataGrid使用。·设计思路 为了让这个控件能够适用于每个页面,定义了一个接口ICurr...

weixin_30654419的博客 140

如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页

概要DataGrid Web 控件有内置的自动或自定义分页功能,而 DataGrid Windows 控件则没有。本文演示了如何为 DataGrid Windows 控件创建简单的分页机制。本文的代码示例利用了 DataSet 对象。在 ADO.NET 中,DataSet 对象是通过单次操作填充的并且永驻在内存中。如果您正在使用一个大型 DataSet,本文将为您介绍如何通过编程按块或页显示数据。

=>Welcome to HW's Column<= 1148

.Net分页控件发布

由于.net自带的分页功能极其死板,自定义性不强不能满足需求俺花了一个多星期的时间 写成的一个.net分页控件特性:1、支持datagrid,datalist等分种数据显示控件,理论上只要有datasource的控件都可以2、支持url方式分页,url分页的每一页都可以被搜索引擎搜到,并且支持url重写3、控件样式可自定义4、封装完全,使用过程序只需写一行代码  1 /********

冷枫@优快云 3124

Web开发框架之分页控件

一直以来,我的博客几乎一直在介绍Winform相关的技术,其中包括Winform的分页控件,其实Web方面的开发一直也在从事,只是较少写这方面的内容而已,经过多年的积累及提炼,也逐渐形成了我自己特色的一套Web开发框架。最近对Web分页控件进行了优化提炼,本文就从Web分页控件进行介绍,让大家了解这个体系中的一部分,也希望提出宝贵的意见,共同探讨。 分页控件的主要特点有: 1)内置支持Orac

jueng 1192

自定义Web DataGrid 翻页控件

下面就是有关DataGrid的源码,本人在此就不在说废话了,有兴趣的朋友可以Download,一看便知。。。using System;using System.ComponentModel;using System.Collections;using System.Collections.Specialized;using System.Text;using System.Text.Regul

danyueren的专栏 721
上一篇: Repeater的应用之嵌套和行操作
下一篇: 打造自己的asp.net验证控件
cpp2017
博客等级 码龄24年 1435粉丝 12原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值