在ASP.NET中,利用GridView自定义分页

 aspx页代码:

<%@ Page Language="VB" MasterPageFile="~/products.master" EnableEventValidation="false" AutoEventWireup="false" CodeFile="more_newproducts.aspx.vb" Inherits="more_newproducts" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    
<table style="width: 576px">
        
<tr>
            
<td style="width: 576px">
    
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        DataKeyNames
="productid" GridLines="Horizontal"
        Width
="576px" Font-Size="13px" HorizontalAlign="Center" CellPadding="0" AllowPaging="True">
        
<Columns>
            
<asp:TemplateField>
                
<ItemTemplate>
                    
<asp:Image ID="Image1" runat="server" Height="80px" ImageUrl='<%# "~/upload/"+Eval("s_imgurl") %>'
                        Width="80px" BorderColor="Silver" BorderStyle="Double" BorderWidth="3px" />
                
</ItemTemplate>
                
<ItemStyle Width="90px" />
            
</asp:TemplateField>
            
<asp:TemplateField HeaderText="productid" InsertVisible="False" SortExpression="productid" Visible="False">
                
<ItemTemplate>
                    
<asp:Label ID="lblproid" runat="server" Text='<%# Bind("productid") %>'></asp:Label>
                </ItemTemplate>
            
</asp:TemplateField>
            
<asp:TemplateField HeaderText="productname" SortExpression="productname" Visible="False">
                
<ItemTemplate>
                    
<asp:Label ID="lblproname" runat="server" Text='<%# Bind("productname") %>'></asp:Label>
                </ItemTemplate>
            
</asp:TemplateField>
            
<asp:TemplateField HeaderText="物品名称">
                
<ItemStyle Width="206px" HorizontalAlign="Left" VerticalAlign="Middle" />
                
<ItemTemplate>
                    
<asp:HyperLink ID="HyperLink1" runat="server" Font-Underline="False" NavigateUrl='<%# Eval("productid", "productdetailsinfo.aspx?productid={0}") %>'
                        Text='<%# Eval("productname") %>'></asp:HyperLink>
                </ItemTemplate>
                
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
            
</asp:TemplateField>
            
<asp:BoundField DataField="pifajia" HeaderText="价格" SortExpression="pifajia" >
                
<ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" />
                
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            
</asp:BoundField>
            
<asp:BoundField DataField="pinpai" HeaderText="品牌" SortExpression="pinpai" >
                
<ItemStyle Width="60px" HorizontalAlign="Center" VerticalAlign="Middle" />
                
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            
</asp:BoundField>
            
<asp:BoundField DataField="changdi" HeaderText="产地" SortExpression="changdi" >
                
<ItemStyle Width="70px" HorizontalAlign="Center" VerticalAlign="Middle" />
                
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            
</asp:BoundField>
            
<asp:TemplateField>
                
<ItemTemplate>
                    
<asp:Button ID="cart" runat="server" CommandName="cart" Text="buy" Width="40px" />
                
</ItemTemplate>
                
<ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" />
            
</asp:TemplateField>
            
<asp:TemplateField>
                
<ItemTemplate>
                    
<asp:Button ID="favorite" runat="server" CommandName="favorite" Text="收藏" Width="40px" />
                
</ItemTemplate>
                
<ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" />
            
</asp:TemplateField>
        
</Columns>
        
<HeaderStyle BackColor="#507CD1" ForeColor="White" Height="22px" />
        
<PagerSettings Visible="False" />
    
</asp:GridView>
            
</td>
        
</tr>
        
<tr>
            
<td style=" font-size :13px" align ="center"  >
                
<asp:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick" runat="server">首 页</asp:LinkButton>
                
<asp:LinkButton ID="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上一页</asp:LinkButton>
                
<asp:LinkButton ID="btnNext" CommandArgument="next" OnClick="PagerButtonClick" runat="server">下一页</asp:LinkButton>
                
<asp:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾 页</asp:LinkButton>
                
<asp:Label ID="LblCurrentIndex" runat="server"></asp:Label>
                
<asp:Label ID="LblPageCount" runat="server"></asp:Label>
                
<asp:Label ID="LblRecordCount" runat="server"></asp:Label>
                
<asp:Label ID="LblNoRecord" runat="server" Text="记录为零" Visible="False"></asp:Label>
            
</td>
        
</tr>
    
</table>
</asp:Content>


aspx.vb代码:

Imports System.Data
Imports System.Data.SqlClient

Partial Class more_newproductsClass more_newproducts
    
Inherits System.Web.UI.Page

    
Protected Sub Page_Load()Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
        GridViewBind()
    
End Sub


    
Protected Sub PagerButtonClick()Sub PagerButtonClick(ByVal sender As ObjectByVal e As EventArgs)

        
Dim arg As String = (CType(sender, LinkButton)).CommandArgument.ToString()

        
Select Case arg

            
Case "prev"
                
If (GridView1.PageIndex > 0Then

                    GridView1.PageIndex 
-= 1
                
End If

            
Case "next"
                
If (GridView1.PageIndex < (GridView1.PageCount - 1)) Then

  &

你可能感兴趣的:(.net,asp.net,asp,vb,VB.NET)