我刚学习.net想请问下怎么实现分页,因为没钱钱买书所以只有到这里请教大家了!
最好有试列~~~!!!非常感谢~~!!!

解决方案 »

  1.   

    你如果想实现简单的分页,其实datagrid就有,另一种方法是从数据库读出时分页。其实就是当前页显示几条记录。读到数据后绑定就行了。
      

  2.   

    DataGridPager.aspx< %@ Page language="c#" Codebehind="DataGridPager.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.DataGridPager" %>
    < !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    < HTML>
     < HEAD>
      < meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
      < meta content="JavaScript" name="vs_defaultClientScript">
      < meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
     < /HEAD>
     < body MS_POSITIONING="GridLayout">
      < form id="Form1" runat="server">
       < TABLE id="Table1" style="Z-INDEX: 101; LEFT: 32px; WIDTH: 480px; POSITION: absolute; TOP: 24px; HEIGHT: 279px"
        cellSpacing="1" cellPadding="1" width="480" border="0">
        < tr>
         < td align="center">
          < h2>DataGrid分页的例子< /h2>
         < /td>
        < /tr>
        < TR>
         < TD style="HEIGHT: 206px" vAlign="top">< asp:datagrid id="DataGridPage" runat="server" GridLines="Horizontal" BackColor="White" BorderStyle="None"
           Width="480px" AllowPaging="True" PageSize="5" PagerStyle-Mode="NumericPages" PagerStyle-HorizontalAlign="Right" BorderColor="#E7E7FF"
           BorderWidth="1px" CellPadding="3" Font-Name="Verdana" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="#eeeeee"
           HorizontalAlign="Center" AutoGenerateColumns="False">
           < SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C">< /SelectedItemStyle>
           < AlternatingItemStyle BackColor="#F7F7F7">
           < ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF">< /ItemStyle>
           < HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="#F7F7F7" BackColor="#4A3C8C">
           < FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE">< /FooterStyle>
           < Columns>
            < asp:BoundColumn DataField="lastname" HeaderText="lastname">
             < HeaderStyle Width="480px">< /HeaderStyle>
            < /asp:BoundColumn>
            < asp:BoundColumn DataField="firstname" HeaderText="firstname">< /asp:BoundColumn>
           < /Columns>
           < PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages">< /PagerStyle>
          < /asp:datagrid>< /TD>
        < /TR>
        < TR>
         < TD align="right">< asp:label id="lblPageCount" runat="server">< /asp:label>< asp:label id="lblCurrentIndex" runat="server">< /asp:label>< asp:linkbutton id="btnFirst" onclick="PageButtonClick" runat="server" Font-Name="verdana" CommandArgument="0">最首页< /asp:linkbutton>< asp:linkbutton id="btnPrev" onclick="PageButtonClick" runat="server" CommandArgument="prev">前一页< /asp:linkbutton>下一页< /asp:linkbutton>最后页< /asp:linkbutton>< /TD>
        < /TR>
       < /TABLE>
      < /form>
     < /body>
    < /HTML>
      

  3.   

    DataGridPager.aspx.csusing 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;
    using System.Data.SqlClient;
    using System.Configuration;
    namespace CommonFunction
    {
     /// 
     /// testDataGridPager 的摘要说明。
     /// 
     public class DataGridPager : System.Web.UI.Page
     {
      protected System.Web.UI.WebControls.Label lblPageCount;
      protected System.Web.UI.WebControls.Label lblCurrentIndex;
      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.DataGrid DataGridPage;
      protected System.Web.UI.WebControls.LinkButton btnLast;
      
      private void Page_Load(object sender, System.EventArgs e)
      {
       //页面初试化时进行数据绑定
       if(!IsPostBack)
        BindGrid();
      }
      //显示当前分页信息
      private void ShowStats()
      {
       //显示当前页面是第几页
       lblCurrentIndex.Text = "第 " + (DataGridPage.CurrentPageIndex + 1).ToString() + " 页";
       //显示总页数
       lblPageCount.Text = "总共 " + DataGridPage.PageCount.ToString() + " 页";
      }  #region Web 窗体设计器生成的代码
      override protected void OnInit(EventArgs e)
      {
       //
       // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
       //
       InitializeComponent();
       base.OnInit(e);
      }
      
      /// 
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改
      /// 此方法的内容。
      /// 
      private void InitializeComponent()
      {    
       this.DataGridPage.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGridPage_Page);
       this.Load += new System.EventHandler(this.Page_Load);  }
      #endregion
      //分别处理”最首页“、“前一页”、“下一页”和“最后页”四个按钮单击时设置DataGrid控件的当前页的索引
      public void PageButtonClick(object sender, EventArgs e)
      {
       //取得按钮单击时传递的命令参数
       string arg = ((LinkButton)sender).CommandArgument.ToString();
       switch(arg)
       {
        //如果点击的是“下一页”
        case "next":
         //如果当前页不是最后一页
         if (DataGridPage.CurrentPageIndex < (DataGridPage.PageCount - 1))
         {
          //设置DataGrid控件的当前页索引为下一页面
          DataGridPage.CurrentPageIndex += 1;
         }
         break;
        //如果点击的是“前一页”
        case "prev":
         //如果当前页不是首页
         if (DataGridPage.CurrentPageIndex > 0)
         {
          //设置DataGrid控件的当前页索引为上一页面
          DataGridPage.CurrentPageIndex -= 1;
         }
         break;
        //如果点击的是“最后页”
        case "last":
         //设置当前页的索引为最后一页
         DataGridPage.CurrentPageIndex = (DataGridPage.PageCount - 1);
         break;
        //默认为”最首页“
        default:
         //设置当前页的索引为首页
         DataGridPage.CurrentPageIndex = System.Convert.ToInt32(arg);
         break;
       }
       BindGrid();
       ShowStats();
      }  private void BindGrid()
      {
       //定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
       SqlConnection cnn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
       //创建数据适配器对象
       SqlDataAdapter da = new SqlDataAdapter("select employeeid,lastname,firstname from employees",cnn);
       //创建DataSet对象
       DataSet ds = new DataSet();
       try
       {
        //填充数据集
        da.Fill(ds, "testTable");
        //进行数据绑定
        DataGridPage.DataSource = ds;
        DataGridPage.DataBind();
       }
       catch(Exception error)
       {
        Response.Write(error.ToString());
       }  
      }  public void DataGridPage_Page(object sender, DataGridPageChangedEventArgs e)
      {
       //设置DataGrid当前页的索引值为用户选择的页的索引
       DataGridPage.CurrentPageIndex = e.NewPageIndex;
       //重新绑定数据
       BindGrid();
       //显示当前分页信息
       ShowStats();
      }
     }
    }
      

  4.   

    其实不管桌面应用、WEB应用还是移动应用,显示数据时一般不用DataGrid,所以一般用不到数据网格中的分页。
      

  5.   

    <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" ShowHeader="False" PageSize="15"
    AllowPaging="True" OnPageIndexChanged="DataGrid1_SelectedIndexChanged">
    <AlternatingItemStyle BackColor="#F1F2F5"></AlternatingItemStyle>
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <%
    if(kind!=1)
    {
    pic = ds.Tables[0].Rows[xp]["Info_Pic"].ToString();
    if(pic==null || pic=="" || pic.Trim()=="")
    {
    pic = "chanping\\No.gif";
    }
    }
    %>
    <table width="550" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <%
    if(kind!=1)
    {
    %>
    <td width="80" rowspan="3" align="center"><img src="<% = pic %>" width="80" height="80" ></td>
    <td width="3" rowspan="3" class="NavTitle"></td>
    <%
    }
    %>
    <td height="22"><span class="TableTop"><a href="Page.aspx?id=<%# DataBinder.Eval(Container.DataItem, "Info_ID") %>"><font color="#000000"><%# DataBinder.Eval(Container.DataItem, "Info_Title") %></font></a></span>
    <font color="#666666">[<strong><%# DataBinder.Eval(Container.DataItem, "Info_Hit") %></strong>]</font></td>
    </tr>
    <tr>
    <td valign="top"><%# DataBinder.Eval(Container.DataItem, "Info_KeyWord") %></td>
    </tr>
    <tr>
    <td valign="top">&nbsp;</td>
    </tr>
    </table>
    <table width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
    <td height="10"><FONT face="宋体"></FONT></td>
    </tr>
    </table>
    <%
    xp++;
    %>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    <PagerStyle Width="500px" Height="22px" HorizontalAlign="Right" ForeColor="Black" Mode="NumericPages"></PagerStyle>
    </asp:DataGrid> protected System.Web.UI.WebControls.DataGrid DataGrid1;
    public int kind;
    public DataSet ds;
    public int xp = 0;
    public string pic;
    public int id;
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    kind = Request.QueryString["kind"] == null ? 0 : int.Parse(Request.QueryString["kind"]);
    id = Request.QueryString["id"] == null ? 0 : int.Parse(Request.QueryString["id"]);
    if(!Page.IsPostBack)
    {
    DataInfosCtl dic = new DataInfosCtl();
    ds = dic.UserKindInfo(kind,id);
    if(ds.Tables[0].Rows.Count!=0)
    {
    DataGrid1.DataSource = ds;
    DataGrid1.DataBind();
    }
    }
    } public void DataGrid1_SelectedIndexChanged(object sender, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    DataInfosCtl dic = new DataInfosCtl();
    ds = dic.UserKindInfo(kind,id);
    DataGrid1.DataSource = ds;
    DataGrid1.DataBind();
    }