hi:
你可以看看
http://www.mastercsharp.com/article.aspx?ArticleID=76&&TopicID=2
Simple Paging in Repeater and DataList Controls或者这里查找,应该有很多关于分页的请赐教

解决方案 »

  1.   

    请看:http://expert.csdn.net/Expert/topic/1544/1544729.xml?temp=.5340235
    上面我的回复
      

  2.   

    下面的示例说明如何在 DataGrid 控件中使用分页。[Visual Basic]
    <%@ Page Language="VB" %>
    <%@ Import Namespace="System.Data" %>
     
    <html>   <script runat="server">
     
          Dim Cart As DataTable
          Dim CartView As DAtaView      Function CreateDataSource() As ICollection         Dim dt As New DataTable()
             Dim dr As DataRow
                
             dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
             dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
             dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
                
             Dim i As Integer
             For i = 0 To 99
                dr = dt.NewRow()
                    
                dr(0) = i
                dr(1) = "Item " + i.ToString()
                dr(2) = 1.23 *(i + 1)
                dt.Rows.Add(dr)
             Next i
                
             Dim dv As New DataView(dt)
             Return dv      End Function 'CreateDataSource      Sub Page_Load(sender As Object, e As EventArgs)
                
             If Not IsPostBack Then
                ' Need to load this data only once.
                ItemsGrid.DataSource = CreateDataSource()
                ItemsGrid.DataBind()
             End If
                
             If CheckBox1.Checked Then
                ItemsGrid.PagerStyle.Mode = PagerMode.NumericPages
             Else
                ItemsGrid.PagerStyle.Mode = PagerMode.NextPrev
             End If       End Sub 'Page_Load      Sub Grid_Change(sender As Object, e As DataGridPageChangedEventArgs)
                
             ' Set CurrentPageIndex to the page the user clicked.
             ItemsGrid.CurrentPageIndex = e.NewPageIndex
                
             ' Rebind the data. 
             ItemsGrid.DataSource = CreateDataSource()
             ItemsGrid.DataBind()      End Sub 'Grid_Change  </script>
     
    <body>
     
       <form runat="server">      <h3>DataGrid Paging Example</h3>
     
          <asp:DataGrid id="ItemsGrid" runat="server"
               BorderColor="black"
               BorderWidth="1"
               CellPadding="3"
               AllowPaging="true"
               AutoGenerateColumns="false"        
               OnPageIndexChanged="Grid_Change">
     
             <HeaderStyle BackColor="#00aaaa">
             </HeaderStyle>
      
             <PagerStyle Mode="NextPrev">
             </PagerStyle>          <Columns>            <asp:BoundColumn 
                     HeaderText="Number" 
                     DataField="IntegerValue"/>
     
                <asp:BoundColumn 
                     HeaderText="Item" 
                     DataField="StringValue"/>            <asp:BoundColumn 
                     HeaderText="Price" 
                     DataField="CurrencyValue" 
                     DataFormatString="{0:c}">
     
                   <ItemStyle HorizontalAlign="right">
                   </ItemStyle>
         
                </asp:BoundColumn>         </Columns>      </asp:DataGrid>      <br>      <asp:CheckBox id="CheckBox1" 
                        Text="Show page navigation"
                        AutoPostBack="true"
                        runat="server"/>
     
       </form>
     
    </body>
    </html>
    [C#]
    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %>
     
    <html>   <script runat="server">
     
          DataTable Cart;
          DataView CartView;
     
          ICollection CreateDataSource() 
          {
             DataTable dt = new DataTable();
             DataRow dr;
     
             dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
             dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
             dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
     
             for (int i = 0; i < 100; i++) 
             {
                dr = dt.NewRow();
     
                dr[0] = i;
                dr[1] = "Item " + i.ToString();
                dr[2] = 1.23 * (i+1); 
                dt.Rows.Add(dr);
             }
     
             DataView dv = new DataView(dt);
             return dv;
          }
     
          void Page_Load(Object sender, EventArgs e) 
          { 
     
             if (!IsPostBack) 
             {
                // Need to load this data only once.
                ItemsGrid.DataSource = CreateDataSource();
                ItemsGrid.DataBind();
             }
     
             if (CheckBox1.Checked)
                ItemsGrid.PagerStyle.Mode = PagerMode.NumericPages;
             else
                ItemsGrid.PagerStyle.Mode = PagerMode.NextPrev;      }
     
          void Grid_Change(Object sender, DataGridPageChangedEventArgs e) 
          {
     
             // Set CurrentPageIndex to the page the user clicked.
             ItemsGrid.CurrentPageIndex = e.NewPageIndex;         // Rebind the data. 
             ItemsGrid.DataSource = CreateDataSource();
             ItemsGrid.DataBind();
          
          }
     
       </script>
     
    <body>
     
       <form runat="server">      <h3>DataGrid Paging Example</h3>
     
          <asp:DataGrid id="ItemsGrid" runat="server"
               BorderColor="black"
               BorderWidth="1"
               CellPadding="3"
               AllowPaging="true"
               AutoGenerateColumns="false"        
               OnPageIndexChanged="Grid_Change">
     
             <HeaderStyle BackColor="#00aaaa">
             </HeaderStyle>
     
             <PagerStyle Mode="NextPrev">
             </PagerStyle>          <Columns>            <asp:BoundColumn 
                     HeaderText="Number" 
                     DataField="IntegerValue"/>
       
                <asp:BoundColumn 
                     HeaderText="Item" 
                     DataField="StringValue"/>            <asp:BoundColumn 
                     HeaderText="Price" 
                     DataField="CurrencyValue" 
                     DataFormatString="{0:c}">
     
                   <ItemStyle HorizontalAlign="right">
                   </ItemStyle>
         
                </asp:BoundColumn>         </Columns>      </asp:DataGrid>      <br>      <asp:CheckBox id="CheckBox1" 
                        Text="Show page navigation"
                        AutoPostBack="true"
                        runat="server"/>
     
       </form>
     
    </body>
    </html>
      

  3.   

    <%@ Page Language="c#" CodeBehind="index.aspx.cs" AutoEventWireup="false" Inherits="example2.index" %>
    <HTML>
    <HEAD>
    </HEAD>
    <body>
    <center>
    <form action="test1.aspx" method="post" runat="server">
    <h3>&nbsp;
    </h3>
    <table width="95%">
    <tbody>
    <tr>
    <td>
    </td>
    </tr>
    <tr>
    <td valign="top">
    <ASP:DataGrid id="MyDataGrid" runat="server" Width="700px" AllowPaging="True" PagerStyle-Mode="NumericPages" PagerStyle-HorizontalAlign="Right" DataKeyField="user_id" AutoGenerateColumns="False" BorderWidth="1px" GridLines="Horizontal" BorderStyle="None" BackColor="White" BorderColor="#E7E7FF" CellPadding="3">
    <SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
    <AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
    <ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
    <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
    <Columns>
    <asp:BoundColumn DataField="user_id" SortExpression="user_id" ReadOnly="True" HeaderText="员工id"></asp:BoundColumn>
    <asp:BoundColumn DataField="user_name" HeaderText="员工名称"></asp:BoundColumn>
    <asp:BoundColumn DataField="password" HeaderText="员工口令"></asp:BoundColumn>
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑">
    <ItemStyle Wrap="False"></ItemStyle>
    </asp:EditCommandColumn>
    <asp:ButtonColumn Text="删除" HeaderText="删除记录" CommandName="Delete"></asp:ButtonColumn>
    <asp:HyperLinkColumn DataNavigateUrlField="user_id" DataNavigateUrlFormatString="javascript:openwindow('detail.aspx?q_id={0}',415,305)" DataTextField="user_id" HeaderText="查看详细" DataTextFormatString="查看..."></asp:HyperLinkColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
    </ASP:DataGrid>
    </td>
    <tr align="right">
    <td><FONT face="宋体">
    <asp:Panel id="pagecon" runat="server" Width="709px">
    <FONT face="宋体">
    <asp:LinkButton id="btnFirst" runat="server" Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="0" Text="首页"></asp:LinkButton></FONT>
    <asp:LinkButton id="btnPrev" runat="server" Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="prev" Text="前页"></asp:LinkButton>
    <asp:LinkButton id="btnNext" runat="server" Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="next" Text="后页"></asp:LinkButton>
    <asp:LinkButton id="btnLast" runat="server" Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="last" Text="末页"></asp:LinkButton>
    <asp:Label id="lblCurrentIndex" runat="server" Font-size="8pt"></asp:Label>
    <asp:Label id="lblPageCount" runat="server" Font-size="8pt"></asp:Label>
    <asp:Checkbox id="chk1" runat="server" Font-Name="Verdana" Text="显示内置的页数" Font-Size="8pt" AutoPostBack="true"></asp:Checkbox>
    </asp:Panel></FONT>
    </td>
    </tr>
    <TR>
    <td valign="top"><FONT face="宋体"></FONT>
    </td>
    </TR>
    </tbody>
    </table>
    </form>
    </center>
    </body>
    </HTML>
      

  4.   

    /********************
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace example2
    {
    /// <summary>
    /// index 的摘要说明。
    /// </summary>
    public class index : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid MyDataGrid;
    protected System.Web.UI.WebControls.Panel pagecon;
    protected System.Web.UI.WebControls.CheckBox chk1;
    protected System.Web.UI.WebControls.Label lblPageCount;
    protected System.Web.UI.WebControls.Label lblCurrentIndex;
    protected System.Web.UI.WebControls.LinkButton btnLast;
    protected System.Web.UI.WebControls.LinkButton btnNext;
    protected System.Web.UI.WebControls.LinkButton btnPrev;
    protected System.Web.UI.WebControls.LinkButton btnFirst;
    public OleDbConnection myConnection;

    private void Page_Load(object sender, System.EventArgs e)
    {
    //判断是否隐藏PagerStyle-Mode
    if (chk1.Checked)
    {
    MyDataGrid.PagerStyle.Visible=true;

    else
    {
    MyDataGrid.PagerStyle.Visible=false;

    //数据库连接
    myConnection = new OleDbConnection("Provider=MSDAORA.1;User ID=test;password=test;Data Source=224;Persist Security Info=False");
        
    if (!IsPostBack)                  
    BindGrid();     
    }
            
    //-------------翻页代码开始
    void PagerButtonClick(Object sender, 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 = Convert.ToInt32(arg);
    break;
    }
    BindGrid();
    }
              
    void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e)
    {
    //处理按下数字的方法
    MyDataGrid.CurrentPageIndex = e.NewPageIndex;
    BindGrid();
    }                   
              
    void ShowStats()
    {
    //显示页面信息
    lblCurrentIndex.Text = "当前页为" + ((int)MyDataGrid.CurrentPageIndex+1);
    lblPageCount.Text = "/共" + MyDataGrid.PageCount + "页";
    }
    //----------------------翻页代码结束
    //删除处理
    public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e)
    {
    String deleteCmd = "DELETE from user_reg where user_id = " +MyDataGrid.DataKeys[(int)e.Item.ItemIndex]; OleDbCommand myCommand = new OleDbCommand(deleteCmd, myConnection);        
    Message.Text= deleteCmd;
    myCommand.Connection.Open(); try
    {
    myCommand.ExecuteNonQuery();
    Message.Text = "<b>已删除记录</b><br>" + deleteCmd;
    }
    catch (OleDbException)
    {
    Message.Text = "错误:未能删除记录";
    Message.Style["颜色"] = "红色";
    } myCommand.Connection.Close(); BindGrid();
    }   
    //修改数据
    public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
    {
    MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
    BindGrid();
    } public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs E)
    {
    MyDataGrid.EditItemIndex = -1;
    BindGrid();
    }
        
    public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs E)
    {        
    String name = ((TextBox)E.Item.Cells[1].Controls[0]).Text;
    String pwd = ((TextBox)E.Item.Cells[2].Controls[0]).Text;
    String updateCmd = "UPDATE user_reg SET user_name ='"+name+"', password ='"+pwd+"' "
    + " where user_id ="+MyDataGrid.DataKeys[(int)E.Item.ItemIndex];  
    OleDbCommand myCommand = new OleDbCommand(updateCmd, myConnection);                                                                       
           
    myCommand.Connection.Open(); try 
    {
    myCommand.ExecuteNonQuery();
    Message.Text = "<b>已更新记录</b><br>" + updateCmd;
    MyDataGrid.EditItemIndex = -1;
    }
    catch (OleDbException ex)
    {
    if (ex.ErrorCode == 2627)
    Message.Text = "错误:已存在具有相同主键的记录";
    else
    Message.Text = "错误:未能更新记录,请确保正确填写了字段<br>id="+ myCommand.Parameters["@id"].Value;          
    Message.Style["color"] = "red";
              
    } myCommand.Connection.Close(); BindGrid();
    }
    //数据绑定
    public void BindGrid()
    {
    OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from user_reg", myConnection);
        
    DataSet ds = new DataSet();
    myCommand.Fill(ds, "user_reg");
    MyDataGrid.DataSource=ds.Tables["user_reg"].DefaultView;
    MyDataGrid.DataBind();
    user_id.Text=GetCurrMenuid().ToString();
    ShowStats();
    }
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.MyDataGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.MyDataGrid_Page);
    this.MyDataGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_Cancel);
    this.MyDataGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_Edit);
    this.MyDataGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_Update);
    this.MyDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_Delete);
    this.btnFirst.Click += new System.EventHandler(this.PagerButtonClick);
    this.btnPrev.Click += new System.EventHandler(this.PagerButtonClick);
    this.btnNext.Click += new System.EventHandler(this.PagerButtonClick);
    this.btnLast.Click += new System.EventHandler(this.PagerButtonClick);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion

    }
    }
      

  5.   

    String strConn=System.Configuration.ConfigurationSettings.AppSettings["eForum"];
    SqlConnection cn=new SqlConnection(strConn);
    cn.Open();
        string strDel ="delete from threads where NickName="+DataGrid1.DataKeys[(int)e.Item.ItemIndex];
    SqlCommand cm=new SqlCommand(strDel,cn);
    cm.ExecuteNonQuery();
    删除的时候怎么包错
    索引超出范围。必须为非负值并小于集合大小。参数名:index 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名:index源错误: 
      

  6.   

    <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="James.UI.WebLesson.WebForm2" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <BODY>
    <form id="WebForm2" method="post" runat="server">
    <table width="85%" border="1">
    <tr>
    <td><asp:label id="lbPageCount" runat="server" ForeColor="Magenta" Font-Size="9pt"></asp:label></td>
    </TD></tr>
    <tr>
    <td align="middle"><asp:datagrid id="DataGrid1" runat="server" Font-Size="9pt" AllowPaging="True" Width="100%" CellPadding="1" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#CC9966">
    <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
    <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
    <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
    <Columns>
    <asp:ButtonColumn Text="选择" CommandName="Select"></asp:ButtonColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
    </asp:datagrid></td>
    </tr>
    <tr>
    <td></td>
    </tr>
    </table>
    </form>
    </BODY>
    </HTML>using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace James.UI.WebLesson
    {
    /// <summary>
    /// WebForm2 的摘要说明。
    /// </summary>
    public class WebForm2 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected System.Web.UI.WebControls.Label lbPageCount;

    private void Page_Load(object sender, System.EventArgs e)
    { BindData();
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
    this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion /// <summary>绑定数据 </summary>
    private void BindData( )
    { DataTable dTable = new DataTable("MyTable"); DataColumnCollection column =  dTable.Columns;
    column.Add("ID",typeof( System.Int32 ));
    column.Add("Name",typeof( System.String ));
    column.Add("Code",typeof( System.String ));             
    DataSet ds = new DataSet();
    ds.Tables.Add( dTable );

    //给表加数据的方法
    for( int i=1;i<=60;i++)
    {
    DataRow dsDR = ds.Tables[0].NewRow();
    dsDR[0] = i;
    dsDR[1] = "Value " + i.ToString();
    dsDR[2] = ("Value" + i.ToString()).GetHashCode().ToString(); ds.Tables[0].Rows.Add( dsDR );
    ds.AcceptChanges();
    }

    DataGrid1.DataSource = ds.Tables[0].DefaultView;
    DataGrid1.DataBind();
    lbPageCount.Text = "共 " + DataGrid1.PageCount.ToString() + " 页,这是第 " + (DataGrid1.CurrentPageIndex+1).ToString() +" 页";
    } private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    BindData( );
    lbPageCount.Text = "共 " + DataGrid1.PageCount.ToString() + " 页,这是第 " + (DataGrid1.CurrentPageIndex+1).ToString() +" 页";

    }
    }
    }