在用GridView里自带的分功能的时候,出现在一个有点奇怪的问题,点下面的页码,会执行上面按钮扭执行的动作,所以就用了一个自定义分页的功能。现将功能描述如下:
aspx页代码:
 <%@ Page Language="VB" MasterPageFile="~/products.master" EnableEventValidation="false" AutoEventWireup="false" CodeFile="more_newproducts.aspx.vb" Inherits="more_newproducts" %>
<%@ 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">
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <table style="width: 576px">
    <table style="width: 576px"> <tr>
        <tr> <td style="width: 576px">
            <td style="width: 576px"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="productid" GridLines="Horizontal"
        DataKeyNames="productid" GridLines="Horizontal" Width="576px" Font-Size="13px" HorizontalAlign="Center" CellPadding="0" AllowPaging="True">
        Width="576px" Font-Size="13px" HorizontalAlign="Center" CellPadding="0" AllowPaging="True"> <Columns>
        <Columns> <asp:TemplateField>
            <asp:TemplateField> <ItemTemplate>
                <ItemTemplate> <asp:Image ID="Image1" runat="server" Height="80px" ImageUrl='<%# "~/upload/"+Eval("s_imgurl") %>'
                    <asp:Image ID="Image1" runat="server" Height="80px" ImageUrl='<%# "~/upload/"+Eval("s_imgurl") %>' Width="80px" BorderColor="Silver" BorderStyle="Double" BorderWidth="3px" />
                        Width="80px" BorderColor="Silver" BorderStyle="Double" BorderWidth="3px" /> </ItemTemplate>
                </ItemTemplate> <ItemStyle Width="90px" />
                <ItemStyle Width="90px" /> </asp:TemplateField>
            </asp:TemplateField> <asp:TemplateField HeaderText="productid" InsertVisible="False" SortExpression="productid" Visible="False">
            <asp:TemplateField HeaderText="productid" InsertVisible="False" SortExpression="productid" Visible="False"> <ItemTemplate>
                <ItemTemplate> <asp:Label ID="lblproid" runat="server" Text='<%# Bind("productid") %>'></asp:Label>
                    <asp:Label ID="lblproid" runat="server" Text='<%# Bind("productid") %>'></asp:Label> </ItemTemplate>
                </ItemTemplate> </asp:TemplateField>
            </asp:TemplateField> <asp:TemplateField HeaderText="productname" SortExpression="productname" Visible="False">
            <asp:TemplateField HeaderText="productname" SortExpression="productname" Visible="False"> <ItemTemplate>
                <ItemTemplate> <asp:Label ID="lblproname" runat="server" Text='<%# Bind("productname") %>'></asp:Label>
                    <asp:Label ID="lblproname" runat="server" Text='<%# Bind("productname") %>'></asp:Label> </ItemTemplate>
                </ItemTemplate> </asp:TemplateField>
            </asp:TemplateField> <asp:TemplateField HeaderText="物品名称">
            <asp:TemplateField HeaderText="物品名称"> <ItemStyle Width="206px" HorizontalAlign="Left" VerticalAlign="Middle" />
                <ItemStyle Width="206px" HorizontalAlign="Left" VerticalAlign="Middle" /> <ItemTemplate>
                <ItemTemplate> <asp:HyperLink ID="HyperLink1" runat="server" Font-Underline="False" NavigateUrl='<%# Eval("productid", "productdetailsinfo.aspx?productid={0}") %>'
                    <asp:HyperLink ID="HyperLink1" runat="server" Font-Underline="False" NavigateUrl='<%# Eval("productid", "productdetailsinfo.aspx?productid={0}") %>' Text='<%# Eval("productname") %>'></asp:HyperLink>
                        Text='<%# Eval("productname") %>'></asp:HyperLink> </ItemTemplate>
                </ItemTemplate> <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" /> </asp:TemplateField>
            </asp:TemplateField> <asp:BoundField DataField="pifajia" HeaderText="价格" SortExpression="pifajia" >
            <asp:BoundField DataField="pifajia" HeaderText="价格" SortExpression="pifajia" > <ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" />
                <ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" /> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:BoundField>
            </asp:BoundField> <asp:BoundField DataField="pinpai" HeaderText="品牌" SortExpression="pinpai" >
            <asp:BoundField DataField="pinpai" HeaderText="品牌" SortExpression="pinpai" > <ItemStyle Width="60px" HorizontalAlign="Center" VerticalAlign="Middle" />
                <ItemStyle Width="60px" HorizontalAlign="Center" VerticalAlign="Middle" /> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:BoundField>
            </asp:BoundField> <asp:BoundField DataField="changdi" HeaderText="产地" SortExpression="changdi" >
            <asp:BoundField DataField="changdi" HeaderText="产地" SortExpression="changdi" > <ItemStyle Width="70px" HorizontalAlign="Center" VerticalAlign="Middle" />
                <ItemStyle Width="70px" HorizontalAlign="Center" VerticalAlign="Middle" /> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:BoundField>
            </asp:BoundField> <asp:TemplateField>
            <asp:TemplateField> <ItemTemplate>
                <ItemTemplate> <asp:Button ID="cart" runat="server" CommandName="cart" Text="buy" Width="40px" />
                    <asp:Button ID="cart" runat="server" CommandName="cart" Text="buy" Width="40px" /> </ItemTemplate>
                </ItemTemplate> <ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" />
                <ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField>
            </asp:TemplateField> <asp:TemplateField>
            <asp:TemplateField> <ItemTemplate>
                <ItemTemplate> <asp:Button ID="favorite" runat="server" CommandName="favorite" Text="收藏" Width="40px" />
                    <asp:Button ID="favorite" runat="server" CommandName="favorite" Text="收藏" Width="40px" /> </ItemTemplate>
                </ItemTemplate> <ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" />
                <ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField>
            </asp:TemplateField> </Columns>
        </Columns> <HeaderStyle BackColor="#507CD1" ForeColor="White" Height="22px" />
        <HeaderStyle BackColor="#507CD1" ForeColor="White" Height="22px" /> <PagerSettings Visible="False" />
        <PagerSettings Visible="False" /> </asp:GridView>
    </asp:GridView> </td>
            </td> </tr>
        </tr> <tr>
        <tr> <td style=" font-size :13px" align ="center"  >
            <td style=" font-size :13px" align ="center"  > <asp:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick" runat="server">首 页</asp:LinkButton>
                <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="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上一页</asp:LinkButton> <asp:LinkButton ID="btnNext" CommandArgument="next" 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:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾 页</asp:LinkButton> <asp:Label ID="LblCurrentIndex" runat="server"></asp:Label>
                <asp:Label ID="LblCurrentIndex" runat="server"></asp:Label> <asp:Label ID="LblPageCount" runat="server"></asp:Label>
                <asp:Label ID="LblPageCount" runat="server"></asp:Label> <asp:Label ID="LblRecordCount" runat="server"></asp:Label>
                <asp:Label ID="LblRecordCount" runat="server"></asp:Label> <asp:Label ID="LblNoRecord" runat="server" Text="记录为零" Visible="False"></asp:Label>
                <asp:Label ID="LblNoRecord" runat="server" Text="记录为零" Visible="False"></asp:Label> </td>
            </td> </tr>
        </tr> </table>
    </table> </asp:Content>
</asp:Content>

aspx.vb代码:
 Imports System.Data
Imports System.Data Imports System.Data.SqlClient
Imports System.Data.SqlClient
 Partial Class more_newproducts
Partial Class more_newproducts Inherits System.Web.UI.Page
    Inherits System.Web.UI.Page
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load GridViewBind()
        GridViewBind() End Sub
    End Sub
 Protected Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
    Protected Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
 Dim arg As String = (CType(sender, LinkButton)).CommandArgument.ToString()
        Dim arg As String = (CType(sender, LinkButton)).CommandArgument.ToString()
 Select Case arg
        Select Case arg
 Case "prev"
            Case "prev" If (GridView1.PageIndex > 0) Then
                If (GridView1.PageIndex > 0) Then
 GridView1.PageIndex -= 1
                    GridView1.PageIndex -= 1 End If
                End If
 Case "next"
            Case "next" If (GridView1.PageIndex < (GridView1.PageCount - 1)) Then
                If (GridView1.PageIndex < (GridView1.PageCount - 1)) Then
 GridView1.PageIndex += 1
                    GridView1.PageIndex += 1 End If
                End If
 Case "last"
            Case "last" GridView1.PageIndex = (GridView1.PageCount - 1)
                GridView1.PageIndex = (GridView1.PageCount - 1)
 Case "first"
            Case "first" GridView1.PageIndex = 0
                GridView1.PageIndex = 0
 End Select
        End Select GridViewBind()
        GridViewBind() End Sub
    End Sub Private Sub GridViewBind()
    Private Sub GridViewBind() Dim conn As New SqlConnection
        Dim conn As New SqlConnection conn.ConnectionString = Application("conn")
        conn.ConnectionString = Application("conn") conn.Open()
        conn.Open() Dim sql As String = "SELECT [productid], [productname], [pifajia], [price], [s_imgurl], [pinpai], [changdi] FROM [products] ORDER BY [fabushijian] DESC"
        Dim sql As String = "SELECT [productid], [productname], [pifajia], [price], [s_imgurl], [pinpai], [changdi] FROM [products] ORDER BY [fabushijian] DESC" Dim dt As DataSet = New DataSet
        Dim dt As DataSet = New DataSet Dim da As SqlDataAdapter = New SqlDataAdapter(sql, conn)
        Dim da As SqlDataAdapter = New SqlDataAdapter(sql, conn) da.Fill(dt)
        da.Fill(dt) GridView1.DataSource = dt.Tables(0).DefaultView
        GridView1.DataSource = dt.Tables(0).DefaultView GridView1.DataBind()
        GridView1.DataBind() LblCurrentIndex.Text = "第<font color='red'>" + (GridView1.PageIndex + 1).ToString() + "</font> 页"
        LblCurrentIndex.Text = "第<font color='red'>" + (GridView1.PageIndex + 1).ToString() + "</font> 页" LblPageCount.Text = "共<font color='red'> " + GridView1.PageCount.ToString() + "</font> 页"
        LblPageCount.Text = "共<font color='red'> " + GridView1.PageCount.ToString() + "</font> 页" LblRecordCount.Text = "总共<font color='red'> " + dt.Tables(0).Rows.Count.ToString() + "</font> 条"
        LblRecordCount.Text = "总共<font color='red'> " + dt.Tables(0).Rows.Count.ToString() + "</font> 条" If (dt.Tables(0).Rows.Count = 0) Then
        If (dt.Tables(0).Rows.Count = 0) Then btnFirst.Visible = False
            btnFirst.Visible = False btnPrev.Visible = False
            btnPrev.Visible = False btnNext.Visible = False
            btnNext.Visible = False btnLast.Visible = False
            btnLast.Visible = False LblCurrentIndex.Visible = False
            LblCurrentIndex.Visible = False LblPageCount.Visible = False
            LblPageCount.Visible = False LblRecordCount.Visible = False
            LblRecordCount.Visible = False LblNoRecord.Visible = True
            LblNoRecord.Visible = True ElseIf (GridView1.PageCount = 1) Then
        ElseIf (GridView1.PageCount = 1) Then btnFirst.Visible = False
            btnFirst.Visible = False btnPrev.Visible = False
            btnPrev.Visible = False btnNext.Visible = False
            btnNext.Visible = False btnLast.Visible = False
            btnLast.Visible = False End If
        End If End Sub
    End Sub
 End Class
End Class 
                     
                    
                 
                    
                 

 
        
 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号