在用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_newproducts
    
Inherits System.Web.UI.Page

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


    
Protected 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

                    GridView1.PageIndex 
+= 1
                
End If

            
Case "last"
                GridView1.PageIndex 
= (GridView1.PageCount - 1)

            
Case "first"
                GridView1.PageIndex 
= 0

        
End Select
        GridViewBind()
    
End Sub

    
Private Sub GridViewBind()
        
Dim conn As New SqlConnection
        conn.ConnectionString 
= Application("conn")
        conn.Open()
        
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 da As SqlDataAdapter = New SqlDataAdapter(sql, conn)
        da.Fill(dt)
        GridView1.DataSource 
= dt.Tables(0).DefaultView
        GridView1.DataBind()
        LblCurrentIndex.Text 
= "第<font color='red'>" + (GridView1.PageIndex + 1).ToString() + "</font> 页"
        LblPageCount.Text 
= "共<font color='red'> " + GridView1.PageCount.ToString() + "</font> 页"
        LblRecordCount.Text 
= "总共<font color='red'> " + dt.Tables(0).Rows.Count.ToString() + "</font> 条"
        
If (dt.Tables(0).Rows.Count = 0Then
            btnFirst.Visible 
= False
            btnPrev.Visible 
= False
            btnNext.Visible 
= False
            btnLast.Visible 
= False
            LblCurrentIndex.Visible 
= False
            LblPageCount.Visible 
= False
            LblRecordCount.Visible 
= False
            LblNoRecord.Visible 
= True
        
ElseIf (GridView1.PageCount = 1Then
            btnFirst.Visible 
= False
            btnPrev.Visible 
= False
            btnNext.Visible 
= False
            btnLast.Visible 
= False
        
End If
    
End Sub


End Class

posted on 2006-11-26 11:42  symjie  阅读(1370)  评论(1编辑  收藏  举报