zoukankan
html css js c++ java
[原创][分页] DataList URL 高效查询翻页
Default.aspx
<
%@ Page Language
=
"
VB
"
AutoEventWireup
=
"
false
"
Debug
=
"
true
"
EnableViewState
=
"
True
"
%
>
<
%@ Import
Namespace =
Namespace
=
"
System.Data
"
%
>
<
%@ Import
Namespace =
Namespace
=
"
System.Data.SqlClient
"
%
>
<
!DOCTYPE html
PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head id
=
"
Head1
"
runat
=
"
server
"
>
<
title
>
DataList
</
title
>
<
script language
=
vb runat
=
server
>
Dim
conn
As
System.Data.SqlClient.SqlConnection
Dim
currentpage
As
Integer
Dim
PageSize, PageCount, TotalCount
As
Integer
Dim
startIndex
As
Integer
Dim
str
, strCount
As
String
Dim
keywords
As
String
Protected
Sub Page_Load()
Sub
Page_Load(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Load
conn
=
New
System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings(
"
doughty_bbsConnectionString
"
).ConnectionString)
conn.Open()
If
Not
Page.IsPostBack
Then
'
第一次页面载入,搜索所有记录
startIndex
=
1
getRecordCount(keywords, startIndex)
Else
Response.Write(
"
第二次载入
"
)
End
If
End Sub
Sub getData()
Sub
getData(
ByVal
str
As
String
)
Dim
mycmd
As
SqlDataAdapter
mycmd
=
New
SqlDataAdapter(
str
, conn)
Dim
ds
As
DataSet
=
New
DataSet
mycmd.Fill(ds,
"
small_class
"
)
DataList1.DataSource
=
ds.Tables(
0
).DefaultView
DataList1.DataBind()
conn.Close()
End Sub
Sub DataList_PageIndex()
Sub
DataList_PageIndex(
ByVal
sender
As
Object
,
ByVal
e
As
DataListItemEventArgs)
'
Dim l As Label = CType(e.Item.FindControl("PageIndex"), Label)
Dim
l
As
Label
l
=
e.Item.FindControl(
"
PageIndex
"
)
If
Not
(l
Is
Nothing
)
Then
l.Text
=
(currentpage
-
1
)
*
PageSize
+
(e.Item.ItemIndex
+
1
)
End
If
End Sub
Sub btnSearch()
Sub
btnSearch(
ByVal
sender
As
Object
,
ByVal
e
As
EventArgs)
'
Server.Transfer("SearchResult.aspx?keywords=" + keywordsTXT.Text.ToString)
'
getRecordCount(keywordsTXT.Text.ToString, startIndex) '不在这边获取本页数据
Dim
s
As
String
=
Request.CurrentExecutionFilePath
+
"
?keywords=
"
+
keywordsTXT.Text.ToString
Response.Redirect(s)
End Sub
Public
Sub getRecordCount()
Sub
getRecordCount(
ByVal
keywords
As
String
,
ByVal
startIndex
As
Integer
)
linkFirst.Enabled
=
True
linkPre.Enabled
=
True
linkNext.Enabled
=
True
linkLast.Enabled
=
True
'
keywords = Server.HtmlEncode(Request.QueryString("keywords"))
'
web.config 中加入 <globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
keywords
=
Trim
(Request.QueryString(
"
keywords
"
))
If
keywords
<>
""
Then
keywords
=
keywords.Replace(
"
'
"
,
"
´
"
)
PageSize
=
2
'
'一页的个数
If
keywords
=
""
Then
strCount
=
"
select count(SmallClass_ID) From [small_class]
"
Else
strCount
=
"
select count(SmallClass_ID) From [small_class] where Memo like '%
"
+
keywords.ToString
+
"
%'
"
End
If
Dim
mycmd
As
SqlCommand
=
New
SqlCommand(strCount, conn)
TotalCount
=
mycmd.ExecuteScalar()
If
TotalCount
Mod
PageSize
=
0
Then
PageCount
=
TotalCount
\
PageSize
Else
PageCount
=
TotalCount
\
PageSize
+
1
End
If
If
Request.QueryString(
"
currentpage
"
)
=
""
Then
currentpage
=
1
If
Request.QueryString(
"
currentpage
"
)
<>
""
Then
currentpage
=
CInt
(Request.QueryString(
"
currentpage
"
))
If
currentpage
<=
0
Then
currentpage
=
1
End
If
lblRecordCount.Text
=
TotalCount.ToString
lblPageCount.Text
=
PageCount.ToString()
If
TotalCount
=
0
Then
lblCurrentPage.Text
=
"
0
"
Else
lblCurrentPage.Text
=
currentpage.ToString
'
当前页
End
If
If
currentpage
<>
1
Then
linkFirst.NavigateUrl
=
Request.CurrentExecutionFilePath
+
"
?keywords=
"
+
keywords
+
"
¤tpage=1
"
linkPre.NavigateUrl
=
Request.CurrentExecutionFilePath
+
"
?keywords=
"
+
keywords
+
"
¤tpage=
"
+
(currentpage
-
1
).ToString
End
If
If
currentpage
<
PageCount
Then
linkNext.NavigateUrl
=
Request.CurrentExecutionFilePath
+
"
?keywords=
"
+
keywords
+
"
¤tpage=
"
+
(currentpage
+
1
).ToString
linkLast.NavigateUrl
=
Request.CurrentExecutionFilePath
+
"
?keywords=
"
+
keywords
+
"
¤tpage=
"
+
PageCount.ToString
End
If
If
TotalCount
=
0
Then
linkFirst.Enabled
=
False
linkPre.Enabled
=
False
linkNext.Enabled
=
False
linkLast.Enabled
=
False
End
If
If
currentpage
=
1
Then
If
keywords
=
""
Then
str
=
"
select top
"
+
PageSize.ToString
+
"
SmallClass_ID,Memo From [small_class]
"
Else
str
=
"
select top
"
+
PageSize.ToString
+
"
SmallClass_ID,Memo From [small_class] where Memo like '%
"
+
keywords.ToString
+
"
%'
"
End
If
Else
If
keywords
=
""
Then
str
=
"
select SmallClass_ID,Memo From [small_class] where SmallClass_ID not In(SELECT TOP
"
&
PageSize
*
(currentpage
-
1
)
&
"
SmallClass_ID FROM small_class ORDER BY SmallClass_ID) and
"
str
=
str
+
"
SmallClass_ID In(SELECT TOP
"
&
(PageSize
*
(currentpage
-
1
)
+
PageSize)
&
"
SmallClass_ID From [small_class] ORDER BY SmallClass_ID) order by SmallClass_ID
"
Else
str
=
"
select SmallClass_ID,Memo From [small_class] where SmallClass_ID not In(SELECT TOP
"
&
PageSize
*
(currentpage
-
1
)
&
"
SmallClass_ID FROM small_class where Memo like '%
"
+
keywords.ToString
+
"
%' ORDER BY SmallClass_ID) and
"
str
=
str
+
"
SmallClass_ID In(SELECT TOP
"
&
(PageSize
*
(currentpage
-
1
)
+
PageSize)
&
"
SmallClass_ID From [small_class] where Memo like '%
"
+
keywords.ToString
+
"
%' ORDER BY SmallClass_ID) order by SmallClass_ID
"
End
If
End
If
strSQL.Text
=
"
关键字:
"
+
keywords
+
"
SQL查询语句:
"
+
str
getData(
str
)
End Sub
</
script
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
method
=
"
post
"
>
<
div
>
关键字:
<
asp:TextBox ID
=
"
keywordsTXT
"
runat
=
"
server
"
></
asp:TextBox
>
<
asp:Button ID
=
"
btn
"
OnClick
=
"
btnSearch
"
runat
=
"
server
"
Text
=
"
Search
"
/>
<
asp:Button ID
=
"
Buttonreload
"
runat
=
"
server
"
Text
=
"
第二次载入
"
/>
<
br
/>
<
asp:DataList ID
=
"
DataList1
"
runat
=
"
server
"
OnItemCreated
=
"
DataList_PageIndex
"
>
<
ItemTemplate
>
<
asp:Label ID
=
"
PageIndex
"
runat
=
"
server
"
></
asp:Label
>
<
%#Eval(
"
Memo
"
)%
>
</
ItemTemplate
>
</
asp:DataList
>
<
asp:Label ID
=
"
strSQL
"
runat
=
"
server
"
ForeColor
=
"
Red
"
></
asp:Label
>
<
br
>
共有记录[
<
asp:label ID
=
"
lblRecordCount
"
runat
=
"
server
"
ForeColor
=
"
Red
"
></
asp:label
>
]条;
页数[
<
asp:Label ID
=
"
lblCurrentPage
"
runat
=
"
server
"
Text
=
"
Label
"
ForeColor
=
"
Red
"
></
asp:Label
>/<
asp:Label ID
=
"
lblPageCount
"
runat
=
"
server
"
Text
=
"
Label
"
></
asp:Label
>
]
<
asp:HyperLink ID
=
"
linkFirst
"
runat
=
"
server
"
>
首页
</
asp:HyperLink
>
|
<
asp:HyperLink ID
=
"
linkPre
"
runat
=
"
server
"
>
上一页
</
asp:HyperLink
>
|
<
asp:HyperLink ID
=
"
linkNext
"
runat
=
"
server
"
>
下一页
</
asp:HyperLink
>
|
<
asp:HyperLink ID
=
"
linkLast
"
runat
=
"
server
"
>
末页
</
asp:HyperLink
>
</
div
>
</
form
>
</
body
>
</
html
>
查看全文
相关阅读:
51nod 1067 Bash游戏 V2
洛谷 P1454 圣诞夜的极光 == codevs 1293 送给圣诞夜的极光
bzoj4754: [Jsoi2016]独特的树叶
bzoj 4241: 历史研究
bzoj 1266 [AHOI2006] 上学路线
bzoj4571: [Scoi2016]美味
bzoj4570: [Scoi2016]妖怪
51nod 1238 最小公倍数之和 V3
一个康托展开的板子
poweroj1745: 餐巾计划问题
原文地址:https://www.cnblogs.com/apiapia/p/1082465.html
最新文章
MVC ActionResult视图结果
MVC Area Usage
洛谷 P1361 小猫爬山
51nod 1130 N的阶乘的长度 V2(斯特林近似)
hdu 1698 Just a Hook
BZOJ 1012: [JSOI2008]最大数maxnumber
codevs 2905 足球晋级
洛谷 P1413 坚果保龄球
hdu 1166 敌兵布阵
洛谷 P1918 保龄球
热门文章
codevs 3070 寻找somebody4(水题日常)
51nod 1105 第K大的数
51nod 1267 4个数和为0
51nod 1096 距离之和最小(水题日常)
洛谷 P1028 数的计算
NYOJ 题目42 一笔画问题
COGS 495. 窗口
洛谷 P1451 求细胞数量
洛谷 P1163 银行贷款
洛谷 P2788 数学1(math1)- 加减算式
Copyright © 2011-2022 走看看