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
>
查看全文
相关阅读:
[BZOJ1433][luogu_P2055][ZJOI2009]假期的宿舍
[BZOJ1280][POJ1149]Emmy卖猪pigs
[BZOJ1066][luogu_P2472][SCOI2007]蜥蜴
[nowcoder_Wannafly挑战赛4_F]线路规划
[SPOJ839]Optimal Marks
[BZOJ1497][NOI2006]最大获利
Django_orm脚本
Function
Class
Python核心编程
原文地址:https://www.cnblogs.com/apiapia/p/1082465.html
最新文章
java动态代理(JDK和cglib)
Spring注解详解
数组的全排列
排队系统实现
洗牌算法
ping过程详解
SIFT+BOW 实现图像检索
latex公式、编号、对齐
Markdown中Latex 数学公式基本语法
VIsual Studio编译OpenCV:无法打开python27_d.lib(python36_d.lib)的问题
热门文章
CTex+WinEdt10.2安装详细教程与破解
机器学习三要素:模型、策略与算法
latex中的希腊字母
HBase详细概述
Hadoop生态系统
Minidump文件分析
[UOJ#132][BZOJ4200][luogu_P2304][NOI2015]小园丁与老司机
[BZOJ2879][Noi2012]美食节
[luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划
[luogu_P2045]方格取数加强版
Copyright © 2011-2022 走看看