GridView分页

 在用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 Class 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

                    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()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

转载于:https://www.cnblogs.com/VirtualMJ/archive/2006/11/07/552749.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值