在用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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
aspx.vb代码:
Imports
System.Data
Imports
System.Data.SqlClient
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
Partial
Class more_newproducts
Class more_newproducts
Inherits System.Web.UI.Page
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GridViewBind()
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Protected Sub PagerButtonClick()Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Dim arg As String = (CType(sender, LinkButton)).CommandArgument.ToString()
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Select Case arg
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case "prev"
If (GridView1.PageIndex > 0) Then
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
GridView1.PageIndex -= 1
End If
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case "next"
If (GridView1.PageIndex < (GridView1.PageCount - 1)) Then
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
GridView1.PageIndex += 1
End If
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case "last"
GridView1.PageIndex = (GridView1.PageCount - 1)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case "first"
GridView1.PageIndex = 0
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Select
GridViewBind()
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
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 = 0) Then
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 = 1) Then
btnFirst.Visible = False
btnPrev.Visible = False
btnNext.Visible = False
btnLast.Visible = False
End If
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Class