Cấp bậc tác giả:

DOTNET

Ví dụ về phân trang trong gridview

Được viết bởi QuangIT ngày 13/08/2012 lúc 07:34 PM
Trong bài này mình sẽ hướng dẫn các bạn viết phân trang đơn giản trong gridview. Với cách chọn trang hiển thị từ DropDownList. Trong ví dụ này hiển thị 10 bản ghi mỗi trang. Khi chọn trang từ DropDownList thì dữ liệu sẽ hiển thị theo trang được chọn.
  • 0
  • 9971
Tải tệp tin: Click ở đây

Ví dụ về phân trang trong gridview

Trong bài này mình sẽ hướng dẫn các bạn viết phân trang đơn giản trong gridview. Với cách chọn trang hiển thị từ DropDownList. Trong ví dụ này hiển thị 10 bản ghi mỗi trang. Khi chọn trang từ DropDownList thì dữ liệu sẽ hiển thị theo trang được chọn.
Dữ liệu minh họa mình lấy từ Access. Để làm phân trang như môt tả sau:
 Để làm được điều đó bạn cần tạo gridview và tạo PageTemplate trong gridview.
Code trang asp.net như sau:

<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>hmweb.com.vn</title>
    <%--create by hungbv@hmweb.com.vn--%>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align: center">
        <asp:Panel ID="Panel1" runat="server" 
        GroupingText="Ví dụ về phân trang Gridview"
            Width="610px">
        <asp:GridView id="CustomersGridView" 
        DataSourceID="AccessDataSource1"   
        autogeneratecolumns="False"
        allowpaging="True"
        ondatabound="CustomersGridView_DataBound"  
        runat="server" DataKeyNames="CustomerID" 
        BackColor="White" BorderColor="#CC9966" 
        BorderStyle="None" BorderWidth="1px" 
        CellPadding="4" Width="100%">
        <PagerStyle forecolor="#330099"
          backcolor="#FFFFCC" HorizontalAlign="Center"/>
        <PagerTemplate>
          <table width="100%">                    
            <tr>                        
              <td style="width:70%">

                <asp:Label id="MessageLabel"
                  forecolor="Blue"
                  text="Chọn trang:" 
                  runat="server"/>
                <asp:DropDownList id="PageDropDownList"
                  autopostback="true"
                  onselectedindexchanged = "PageDropDownList_SelectedIndexChanged" 
                  runat="server"/>
              </td>   
              <td style="width:70%; text-align:right">
                <asp:Label id="CurrentPageLabel"
                  forecolor="Blue"
                  runat="server"/>
              </td>
            </tr>                    
          </table>
        </PagerTemplate> 
          <Columns>
              <asp:BoundField DataField="CustomerID" 
              HeaderText="CustomerID" ReadOnly="True" 
              SortExpression="CustomerID" />
              <asp:BoundField DataField="CompanyName" 
              HeaderText="CompanyName" SortExpression="CompanyName" />
              <asp:BoundField DataField="Address" 
              HeaderText="Address" SortExpression="Address" />
              <asp:BoundField DataField="City" 
              HeaderText="City" SortExpression="City" />
          </Columns>
            <RowStyle BackColor="White" ForeColor="#330099" />
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
      </asp:GridView>
        </asp:Panel>
        <asp:AccessDataSource ID="AccessDataSource1" 
        runat="server" DataFile="~/SP.mdb" 
        SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [Address], [City] FROM [Customers]">
        </asp:AccessDataSource>
    </div>
    </form>
</body>
</html>
 
Trong code behind bạn làm như sau:
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;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
    {
        GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
        DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
        // Thiết lập thuộc tính PageIndex Để hiển thị trang đuợc chọn.
        CustomersGridView.PageIndex = pageList.SelectedIndex;
    }

    protected void CustomersGridView_DataBound(Object sender, EventArgs e)
    {
        GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
        DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
        Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");

        if (pageList != null)
        {
            for (int i = 0; i < CustomersGridView.PageCount; i++)
            {
                int pageNumber = i + 1;
                ListItem item = new ListItem(pageNumber.ToString());
                if (i == CustomersGridView.PageIndex)
                {
                    item.Selected = true;
                }
                pageList.Items.Add(item);
            }
        }

        if (pageLabel != null)
        {

            int currentPage = CustomersGridView.PageIndex + 1;
            // Cập nhật thông tin lable hiển thị trang
            pageLabel.Text = "Page " + currentPage.ToString() +
              " of " + CustomersGridView.PageCount.ToString();
        }
    }
}
Bạn có thể download mã nguồn về tham khảo

Nguồn bài viết: hmweb

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML