在网上看到的一个分页带跳转的分页例子.有一个用户控件!我调试一下不行.看看什么原因?default.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>
<%@ Register TagPrefix="user" TagName="ChangePage" Src="ChangePage.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <user:ChangePage id="tcChangePage" runat="server" />
    </form>
</body>
</html>
default.aspx.csusing System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace Education.Web
{
    /// <summary>
    /// 维护页面
    /// </summary>
    public class TeachingCenterList : System.Web.UI.Page
    {
        //声明翻页用户控件 注意:名称tcChangePage应该和拖拽过来后的界面中的ChangePage控件的ID相同        protected ChangePage tcChangePage;        protected System.Web.UI.WebControls.DataGrid MyDataGrid; //要翻页的DataGrid控件的名称        public void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //网格数据绑定
                BindGrid();
            }
        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);            //翻页控件中的事件声明   
            tcChangePage.PageIndexChanged += new ChangePage.PageIndexChangedEventHandler(this.PageButtonClick); //翻页事件
            tcChangePage.GoPage += new ChangePage.GoPageEventHandler(this.PageGoButtonClick);     //GoTo事件
        }
        #endregion        /// 网格数据绑定
        void BindGrid()
  {   
   //读取数据库的信息,获得DataView    将DataView绑定到DataGrid上去   {....}      
    
   //显示页码
   tcChangePage.ShowStats( MyDataGrid );
   //判断翻页按钮Enable状态
   tcChangePage.PagerButtonEnable( MyDataGrid );
  }        #region 翻页处理 代码的主体部分
        /// 翻页按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void PageButtonClick(Object sender, System.EventArgs e)
        {
            //获得LinkButton的参数值
            String arg = ((LinkButton)sender).CommandArgument;            switch (arg)
            {
                case ("next"): //下页
                    if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
                        MyDataGrid.CurrentPageIndex++;
                    break;
                case ("prev"): //上页
                    if (MyDataGrid.CurrentPageIndex > 0)
                        MyDataGrid.CurrentPageIndex--;
                    break;
                case ("last"): //末页
                    MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
                    break;
                default:       //首页     
                    MyDataGrid.CurrentPageIndex = 0;
                    break;
            }
            //网格数据绑定
            BindGrid();
            //网格数据指定
            MyDataGrid.SelectedIndex = 0;
            //指定到网格中第一行,同时将数据显示到明细窗口(显示)
            SelectItemCommand(0, "显示");
        }
        /// <summary>
        /// 翻页Go按钮处理事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="PageIndex"></param>
        public void PageGoButtonClick(Object sender, int PageIndex)
        {
            //获得Button的参数值
            string arg = ((Button)sender).CommandArgument;
            switch (arg)
            {
                case ("GoPage"): //指定到某一页
                    if (PageIndex <= MyDataGrid.PageCount)
                    {
                        MyDataGrid.CurrentPageIndex = (PageIndex - 1);
                    }
                    else
                    {
                        MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
                    }
                    break;
            }
            //网格数据绑定
            BindGrid();
            //网格数据指定
            MyDataGrid.SelectedIndex = 0;
            //指定到网格中第一行,同时将数据显示到明细窗口(显示)
            SelectItemCommand(0, "显示");
        }
        #endregion
    }
}

解决方案 »

  1.   

    ChangePage.ascx<%@ Control Language="c#" AutoEventWireup="false" Codebehind="ChangePage.ascx.cs" Inherits="Education.Web.ChangePage" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    <script language="javascript">
    <!--
    function val_num(field) {
      if (field.value=="")
      {
       alert("错误!\n\n请输入正确数字!");
       field.focus();    
       field.select();
       return false;
      }  
      else if (field.value.indexOf(" ")>=0)
      {
       alert("错误!\n\n请输入正确数字!\n勿包含空格!");
       field.focus();    
       field.select();
       return false;
      }
      else if (isNaN(field.value)) 
      { 
       alert("错误!\n\n请输入正确数字!");
       field.focus();    
       field.select();
       return false;
      }
      else if (field.value.indexOf('.')>=0)
      {
       alert("错误!\n\n请输入整数!")
       field.focus();    
       field.select();
       return false;
      }
      else if (parseInt(field.value)<=0)
      {
       alert("错误!\n\n请输入正确数字!");
       field.focus();    
       field.select();
       return false;
      }
      return true;
    }
    //-->
    </script>
    <TABLE id="Table11" cellSpacing="0" cellPadding="0" width="100%" border="0">
     <tr>
      <td align="right"><asp:linkbutton id="btnFirst" runat="server" CssClass="Linkbutton" Text="首页" CommandArgument="0">首页</asp:linkbutton><asp:linkbutton id="btnPrev" runat="server" CssClass="Linkbutton" Text="前页" CommandArgument="prev">
       前页</asp:linkbutton><asp:linkbutton id="btnNext" runat="server" CssClass="Linkbutton" Text="后页" CommandArgument="next">
       后页</asp:linkbutton><asp:linkbutton id="btnLast" runat="server" CssClass="Linkbutton" Text="末页" CommandArgument="last">
       
       末页</asp:linkbutton>&nbsp;&nbsp;
       <asp:label id="lblGoPage" runat="server">转到:</asp:label><asp:textbox id="txtGoPage" runat="server" Width="25px" CssClass="text"></asp:textbox>&nbsp;<asp:label id="lblPage" runat="server">页</asp:label>
       <asp:button id="btnGo" runat="server" CssClass="buttonShort" Text="Go" CommandArgument="GoPage"></asp:button>&nbsp;&nbsp;<asp:label id="lblCurrentIndex" runat="server"></asp:label></td>
     </tr>
    </TABLE>
    ChangePage.ascx.csnamespace Education.Web
    {
     using System;
     using System.Data;
     using System.Drawing;
     using System.Web;
     using System.Web.UI.WebControls;
     using System.Web.UI.HtmlControls; /// <summary>
     /// 自定义用户控件,用来显示Grid分页
     /// </summary>
     public abstract class ChangePage : System.Web.UI.UserControl
     {
      protected System.Web.UI.WebControls.LinkButton btnFirst;
      protected System.Web.UI.WebControls.LinkButton btnPrev;
      protected System.Web.UI.WebControls.LinkButton btnNext;
      protected System.Web.UI.WebControls.LinkButton btnLast;
      protected System.Web.UI.WebControls.Label lblGoPage;
      protected System.Web.UI.WebControls.TextBox txtGoPage;
      protected System.Web.UI.WebControls.Label lblPage;
      protected System.Web.UI.WebControls.Button btnGo;
      protected System.Web.UI.WebControls.Label lblCurrentIndex;
      //声明翻页事件
      public delegate void PageIndexChangedEventHandler(object sender, System.EventArgs e);
      public event PageIndexChangedEventHandler PageIndexChanged;
      //声明GoTo事件
      public delegate void GoPageEventHandler(object sender, int PageIndex);
      public event GoPageEventHandler GoPage;  protected virtual void OnPageIndexChanged(object sender,System.EventArgs  e)
      {
       if (PageIndexChanged != null)
        PageIndexChanged(sender, e);
      }  protected virtual void OnGoPage(object sender,int PageIndex)
      {
       if (GoPage != null)
        GoPage(sender, PageIndex);
      }  private void Page_Load(object sender, System.EventArgs e)
      {   
       //判断txtGoPage输入的是否为数字
       this.btnGo.Attributes.Add("onclick","return val_num("+this.txtGoPage.ClientID.ToString()+")");   
      }    
      //设置翻页按钮的Enabled属性
      public void PagerButtonEnable(System.Web.UI.WebControls.DataGrid dgPage)
      {
       btnFirst.Enabled = (dgPage.CurrentPageIndex == 0 ? false : true);
       btnPrev.Enabled  = (dgPage.CurrentPageIndex == 0 ? false : true);   
       btnNext.Enabled = (dgPage.CurrentPageIndex == (dgPage.PageCount - 1) ? false : true);
       btnLast.Enabled = (dgPage.CurrentPageIndex == (dgPage.PageCount - 1) ? false : true);     
      }
      //显示页面信息
      public void ShowStats(System.Web.UI.WebControls.DataGrid dgPage)
      {
       
       lblCurrentIndex.Text = "页码:[ " + ((int)dgPage.CurrentPageIndex+1) + " / "+ dgPage.PageCount + " ]" ;   
      }
      //首页
      private void btnFirst_Click(object sender, System.EventArgs e)
      {
       OnPageIndexChanged( sender , e );
      }
      //上页
      private void btnPrev_Click(object sender, System.EventArgs e)
      {
       OnPageIndexChanged( sender , e );
      }
      //下页
      private void btnNext_Click(object sender, System.EventArgs e)
      {
       OnPageIndexChanged( sender , e );
      }
      //末页
      private void btnLast_Click(object sender, System.EventArgs e)
      {
       OnPageIndexChanged( sender , e );
      }    
      //GoTo到某一页
      private void btnGo_Click(object sender, System.EventArgs e)
      {
       OnGoPage( sender ,Convert.ToInt32(this.txtGoPage.Text) );
      }
      #region Web Form Designer generated code
      override protected void OnInit(EventArgs e)
      {
       //
       // CODEGEN: This call is required by the ASP.NET Web Form Designer.
       //
       InitializeComponent();
       base.OnInit(e);
      }
      
      ///  Required method for Designer support - do not modify
      ///  the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
       this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
       this.btnPrev.Click += new System.EventHandler(this.btnPrev_Click);
       this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
       this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
       this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
       this.Load += new System.EventHandler(this.Page_Load);  }
      #endregion
     }
    }3Q!