程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 解析GridView自帶分頁及與DropDownList結合使用,gridview自帶分頁

解析GridView自帶分頁及與DropDownList結合使用,gridview自帶分頁

編輯:關於.NET

解析GridView自帶分頁及與DropDownList結合使用,gridview自帶分頁


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數據的函數
  }
}

總結下,方便以後用到。

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持幫客之家!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved