求一个gridview分页代码
不需要存储过程的
谢谢了
 先给100  如果不够 可以在加 
谢谢大家

解决方案 »

  1.   

    http://www.webdiyer.com/AspNetPager/default.aspx
    分页控件里面有gridview的示例
      

  2.   

    孟宪会的
    http://dotnet.aspx.cc/article/7919da6b-9014-41c2-b9b6-10e5ec047f7d/read.aspx
      

  3.   

    哦 对了 不一定是gridview控件
    只要是自定义分页的都可以 
      

  4.   


     protected void gvBase_RowCreated(object sender, GridViewRowEventArgs e)
        {//分页代码
            int PageSize = this.gvBase.PageSize;
            int PageIndex = this.gvBase.PageIndex;
            int PageCount = this.gvBase.PageCount;
            int RecoderCount = this.gvBase.Rows.Count;        if (e.Row.RowType == DataControlRowType.Pager)
            {
                LinkButton Frist = new LinkButton();
                LinkButton Prev = new LinkButton();
                LinkButton Next = new LinkButton();
                LinkButton Last = new LinkButton();
                e.Row.Controls.Clear();
                TableCell tc = new TableCell();
                tc.Controls.Add(new LiteralControl("  "));
                tc.Controls.Add(new LiteralControl("共" + RecoderCount.ToString() + "条记录"));
                tc.Controls.Add(new LiteralControl("  "));
                tc.Controls.Add(new LiteralControl("共" + PageCount.ToString() + "页"));
                tc.Controls.Add(new LiteralControl("  "));
                tc.Controls.Add(new LiteralControl("当前第" + (PageIndex + 1).ToString() + "页"));
                tc.Controls.Add(new LiteralControl("  "));
                tc.Controls.Add(new LiteralControl("每页" + PageSize.ToString() + "条记录"));
                tc.Controls.Add(new LiteralControl("    "));            Frist.Text = "首页";
                Frist.CommandName = "Page";
                Frist.CommandArgument = "First";
                Frist.Font.Underline = false;            Prev.Text = "上一页";
                Prev.CommandName = "Page";
                Prev.CommandArgument = "Prev";
                Prev.Font.Underline = false;            Next.Text = "下一页";
                Next.CommandName = "Page";
                Next.CommandArgument = "Next";
                Next.Font.Underline = false;            Last.Text = "尾页";
                Last.CommandName = "Page";
                Last.CommandArgument = "Last";
                Last.Font.Underline = false;            if (PageIndex <= 0)
                {
                    Prev.Enabled = false;
                    Frist.Enabled = false;
                }
                else
                {
                    Prev.Enabled = true;
                    Frist.Enabled = true;
                }
                tc.Controls.Add(Frist);
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                tc.Controls.Add(Prev);
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));            for (int i = 0; i < PageCount; i++)
                {
                    if (i == PageIndex)
                    {
                        tc.Controls.Add(new LiteralControl("<span style='color:red;font-weight:bold'>" + (i + 1).ToString() + "</span>"));                }
                    else
                    {
                        LinkButton lbBtn = new LinkButton();
                        lbBtn.Text = (i + 1).ToString();
                        lbBtn.CommandName = "Page";
                        lbBtn.CommandArgument = (i + 1).ToString();
                        lbBtn.Font.Underline = false;
                        tc.Controls.Add(lbBtn);
                    }
                    tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                }
                if (PageIndex >= PageCount - 1)
                {
                    Next.Enabled = false;
                    Last.Enabled = false;
                }
                else
                {
                    Next.Enabled = true;
                    Last.Enabled = true;
                }
                tc.Controls.Add(Next);
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                tc.Controls.Add(Last);
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));            e.Row.Controls.Add(tc);
            }
        }
      

  5.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;public partial class gv4 : System.Web.UI.Page
    {
         protected void Page_Load(object sender, EventArgs e)
         {
             if (!IsPostBack)
             {
                 bind();    
             }
            
         }
         private void bind()
         { //建立一个方法,用与在页面中为控件绑定数据
             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);
             //初始化连接
             SqlDataAdapter sda = new SqlDataAdapter("select * from authors",con);
             DataSet ds = new DataSet();
             sda.Fill(ds,"authors");         GridView1.DataSource = ds.Tables["authors"];         GridView1.AllowPaging = true;//设置他可以分页,前台必须设置分页模板不可见,否则他要使坏
             GridView1.PageSize = 5;//分页大小为10
             GridView1.DataBind();         if (GridView1.PageIndex == 0)
             {//如果当前为首页,那么上页和首页按纽不可用
                 Button1.Enabled = false;
                 Button2.Enabled = false;
             }
             else
             {
                 Button1.Enabled = true;
                 Button2.Enabled = true;
             }
             if (GridView1.PageIndex == GridView1.PageCount-1)
             {//如果当前为末页,那么下页和末页按纽不可用
                 Button3.Enabled = false;
                 Button4.Enabled = false;
             }
             else
             {
                 Button3.Enabled = true;
                 Button4.Enabled = true;
             }
         }
         private int cmd(string query)
         {//页内多次涉及到修改删除等要使用到这个,所以就做成一个方法
             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);
             //初始化连接
             con.Open();
             SqlCommand cmd = new SqlCommand(query,con);
             return cmd.ExecuteNonQuery();
         }
         protected void Button1_Click(object sender, EventArgs e)
         {//实现分页
             switch (((Button)sender).CommandArgument.ToString())
             {
                 case "first":
                     GridView1.PageIndex = 0;
                     break;
                 case "last":
                     GridView1.PageIndex = GridView1.PageCount-1;
                     break;
                 case "prev":
                     GridView1.PageIndex = GridView1.PageIndex - 1;
                     break;
                 case "next":
                     GridView1.PageIndex = GridView1.PageIndex + 1;
                     break;
             }
             bind();
         }
         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
         {//进入编辑模式
             GridView1.EditIndex = e.NewEditIndex;
             bind();
         }
         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
         {//编辑动作
             string id = GridView1.DataKeys[e.RowIndex]["au_id"].ToString();
             string fname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox1")).Text;
             //注意上面,我之所以找到了文本框的名字,那是因为我把所有的列都转换成了模板列,不怕你笑,我整了至少一个小时才整出来
             //但是编辑他的数据根本没有这么麻烦,我们即将学到的DetailsView和FromView可以轻松完成这个任务.
             //这里我硬着头皮做出来只是因为给有些喜欢玩稀奇的朋友提个思路。
             string city = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox2")).Text;
             string phone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox3")).Text;
             string query = "update authors set au_fname='" + fname + "',city='" + city + "',phone='"+phone+"' where au_id='" + id + "'";
             if (cmd(query) > 0)
             {
                 GridView1.EditIndex = -1;
                 bind();
             }
         }
         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
         {//cancel事件
             GridView1.EditIndex = -1;
             bind();
         }
         protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
         {//删除事件
             string id = GridView1.DataKeys[e.RowIndex]["au_id"].ToString();
             //得到该行的主键
             string query = "delete from authors where au_id='"+id+"'";
             if (cmd(query) > 0)
             {
                 bind();
             }
         }
    }
      

  6.   

    要在属性里设置启动分页,和每页的数据数,和显示的BUTTON数
      

  7.   

    GridView的分页是自带的:
    1.设置GridView控件AllowPaging属性为true;
    2.创建GridView的PageIndexChanging事件
      protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
     {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindData();-重新绑定方法
     }  public void  BindData()
     {
            this.GridView1.DataSource = 要绑定的数据;        
            this.GridView1.DataBind();
     }
      

  8.   

    <asp:datagrid id="dgMsgList" runat="server" PageSize="10" DataKeyField="ID" OnPageIndexChanged="DataGrid_PageChanged"
    PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NumericPages" AllowPaging="True" AutoGenerateColumns="False" BorderColor="#93BEE2"
    BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" AllowSorting="True" Width="100%">
    <SelectedItemStyle Font-Bold="True" ForeColor="#EBF5EB" BackColor="#008A8C"></SelectedItemStyle>
    <AlternatingItemStyle BackColor="#E8F4FF"></AlternatingItemStyle>
    <ItemStyle Font-Size="X-Small" HorizontalAlign="Center" ForeColor="Black" VerticalAlign="Middle"
    BackColor="White"></ItemStyle>
    <HeaderStyle Font-Size="X-Small" HorizontalAlign="Center" Height="10px" ForeColor="green" VerticalAlign="Top"
    BackColor="#E8F4FF"></HeaderStyle>
    <FooterStyle Font-Size="XX-Small" HorizontalAlign="Center" Height="10px" ForeColor="Black" VerticalAlign="Bottom"
    BackColor="#93BEE2"></FooterStyle>
                           
                                  <columns>
                                  <asp:TemplateColumn HeaderText="选择">
                                    <headerstyle HorizontalAlign="Center" Width="7%"></headerstyle>
                                    <itemstyle Font-Size="X-Small" HorizontalAlign="Center" Height="20px" Width="60px"></itemstyle>
                                    <itemtemplate>
                                      <asp:CheckBox ID="grpMsgID" Checked="False" runat="server"></asp:CheckBox>
                                    </itemtemplate>
                                  </asp:TemplateColumn>
     </columns>
                                </asp:datagrid>
    public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgMsgList.CurrentPageIndex = e.NewPageIndex;
            数据帮定
    }
      

  9.   

    <PagerTemplate>
                    <table width="100%">
                              <tr>
                                <td style="text-align:right;text-decoration: none">
                                第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>' />页
                                    共<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount  %>' />页 
                                    <asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" style="color: #ffffff; text-decoration:none;" />
                                  <asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" style="color: #ffffff; text-decoration:none;" />
                                 <asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" style="color: #ffffff; text-decoration:none;" />                          
                                 <asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" style="color: #ffffff; text-decoration:none;" />                                            
                                 <asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>' />
                                 <asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO"  style="color: #ffffff; text-decoration:none;"/><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->
                                 </td>
                              </tr>
                            </table>
                    </PagerTemplate>
            GridView theGrid = sender as GridView;  // refer to the GridView
            int newPageIndex = 0;        if (-2 == e.NewPageIndex)
            { // when click the "GO" Button
                TextBox txtNewPageIndex = null;
                //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                GridViewRow pagerRow = GV_kh.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
                //updated at 2006年6月21日3:15:33            if (null != pagerRow)
                {
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                }            if (null != txtNewPageIndex)
                {
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                }
            }
            else
            {  // when click the first, last, previous and next Button
                newPageIndex = e.NewPageIndex;
            }        // check to prevent form the NewPageIndex out of the range
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= GV_kh.PageCount ? GV_kh.PageCount - 1 : newPageIndex;        // specify the NewPageIndex
            GV_kh.PageIndex = newPageIndex;        Bind();//数据绑定
      

  10.   

     protected void gvBase_RowCreated(object sender, GridViewRowEventArgs e)
        {//分页代码
            int PageSize = this.gvBase.PageSize;
            int PageIndex = this.gvBase.PageIndex;
            int PageCount = this.gvBase.PageCount;
            int RecoderCount = this.gvBase.Rows.Count;        if (e.Row.RowType == DataControlRowType.Pager)
            {
                LinkButton Frist = new LinkButton();
                LinkButton Prev = new LinkButton();
                LinkButton Next = new LinkButton();
                LinkButton Last = new LinkButton();
                e.Row.Controls.Clear();
                TableCell tc = new TableCell();
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                tc.Controls.Add(new LiteralControl("共" + RecoderCount.ToString() + "条记录"));
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                tc.Controls.Add(new LiteralControl("共" + PageCount.ToString() + "页"));
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                tc.Controls.Add(new LiteralControl("当前第" + (PageIndex + 1).ToString() + "页"));
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                tc.Controls.Add(new LiteralControl("每页" + PageSize.ToString() + "条记录"));
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));            Frist.Text = "首页";
                Frist.CommandName = "Page";
                Frist.CommandArgument = "First";
                Frist.Font.Underline = false;            Prev.Text = "上一页";
                Prev.CommandName = "Page";
                Prev.CommandArgument = "Prev";
                Prev.Font.Underline = false;            Next.Text = "下一页";
                Next.CommandName = "Page";
                Next.CommandArgument = "Next";
                Next.Font.Underline = false;            Last.Text = "尾页";
                Last.CommandName = "Page";
                Last.CommandArgument = "Last";
                Last.Font.Underline = false;            if (PageIndex <= 0)
                {
                    Prev.Enabled = false;
                    Frist.Enabled = false;
                }
                else
                {
                    Prev.Enabled = true;
                    Frist.Enabled = true;
                }
                tc.Controls.Add(Frist);
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                tc.Controls.Add(Prev);
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));            for (int i = 0; i < PageCount; i++)
                {
                    if (i == PageIndex)
                    {
                        tc.Controls.Add(new LiteralControl("<span style='color:red;font-weight:bold'>" + (i + 1).ToString() + "</span>"));                }
                    else
                    {
                        LinkButton lbBtn = new LinkButton();
                        lbBtn.Text = (i + 1).ToString();
                        lbBtn.CommandName = "Page";
                        lbBtn.CommandArgument = (i + 1).ToString();
                        lbBtn.Font.Underline = false;
                        tc.Controls.Add(lbBtn);
                    }
                    tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                }
                if (PageIndex >= PageCount - 1)
                {
                    Next.Enabled = false;
                    Last.Enabled = false;
                }
                else
                {
                    Next.Enabled = true;
                    Last.Enabled = true;
                }
                tc.Controls.Add(Next);
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                tc.Controls.Add(Last);
                tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));            e.Row.Controls.Add(tc);
            }
        }
      

  11.   

    http://dotnet.aspx.cc/article/7919da6b-9014-41c2-b9b6-10e5ec047f7d/read.aspx
      

  12.   


    把GridView 的属性设置好,他自动分页...如下:
    <asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize ="10"  OnPageIndexChanging="paging">
            </asp:GridView>下面这个函数自己写内容....
    protected void paging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindData();
        }