欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解析GridView自带分页及与DropDownList结合使用

程序员文章站 2022-08-27 16:06:48
gridview自带的分页功能实现: 要实现grdview分页的功能 操作如下: 更改grdview控件的allowpaging属性为true。 更改g...

gridview自带的分页功能实现:

要实现grdview分页的功能

操作如下:

  1. 更改grdview控件的allowpaging属性为true。
  2. 更改grdview控件的pagesize属性为 任意数值(默认为10)
  3. 更改grdview控件的pagesetting->mode为numeric等(默认为numeric)该属性为分页样式。

gridview属性设置好了,从页面上也能看到分页样式。

现在开始实现分页的功能:

  1. 在<<asp:gridview id=......>后添加,onpageindexchanging="gridview1_pageindexchanging"
  2. 在对应的aspx.cs中添加:
  protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)
  {
    gridview1.pageindex = e.newpageindex;
    initpage(); //重新绑定gridview数据的函数
  }

参考代码:

<%@ page language="c#" autoeventwireup="true" codefile="gridview_zidaifenye.aspx.cs" inherits="gridview_zidaifenye" %>
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
   <asp:gridview id="gridview1" runat="server" allowpaging="true" allowsorting="true"
      autogeneratecolumns="false" datakeynames="id" pagersettings-mode="numeric"
      onrowdatabound="gridview1_rowdatabound" pagesize="5" onpageindexchanging="gridview1_pageindexchanging">
      <columns>
        <asp:boundfield datafield="id" headertext="id" insertvisible="false" readonly="true"
          sortexpression="id" />
        <asp:boundfield datafield="name" headertext="name" sortexpression="name" />
        <asp:boundfield datafield="stock" headertext="stock" sortexpression="stock" />
      </columns>
    </asp:gridview>
    每页显示<asp:dropdownlist id="dropdownlist1" runat="server" autopostback="true" onselectedindexchanged="dropdownlist1_selectedindexchanged">
      <asp:listitem>5</asp:listitem>
      <asp:listitem>10</asp:listitem>
      <asp:listitem>15</asp:listitem>
    </asp:dropdownlist>
    条记录     
    <asp:label id="lblmsg" runat="server"></asp:label>
  </div>
  </form>
</body>
</html>

后台代码:

using system;
using system.collections.generic;
using system.data;
using system.data.sqlclient;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
public partial class gridview_zidaifenye : system.web.ui.page
{
  dbaccess db = new dbaccess();
  protected void page_load(object sender, eventargs e)
  {
    if (!ispostback)
    {
      // gvproduct.datasource = getdata();
      // gvproduct.databind();
      bindgrid();
    }
  }
  public void bindgrid()
  {
    sqlcommand comm = db.createcommand("select * from product p,uuser u where p.userid=u.id");
    sqldataadapter sda = new sqldataadapter();
    sda.selectcommand = comm;
    dataset ds = new dataset();
    sda.fill(ds,"datatable");
    dataview dv = ds.tables[0].defaultview;
    gridview1.datasource = dv;
    gridview1.databind();
  }
  protected void dropdownlist1_selectedindexchanged(object sender, eventargs e)
  {
    gridview1.pagesize = int.parse(dropdownlist1.selectedvalue);
    gridview1.pageindex = 0;
    bindgrid(); //gridview1.databind();
  }
  protected void gridview1_rowdatabound(object sender, gridviewroweventargs e)
  {
    lblmsg.text = "当前页为第" + (gridview1.pageindex + 1).tostring() + "页,共" + (gridview1.pagecount).tostring() + "页";
  }
  protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)
  {
    gridview1.pageindex = ((gridviewpageeventargs)e).newpageindex;
    bindgrid(); //重新绑定gridview数据的函数
  }
}

总结下,方便以后用到。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!