.net搜索查询并实现分页实例
程序员文章站
2024-03-02 22:51:40
前台:复制代码 代码如下:<%@ page language="c#" autoeventwireup="true" codebehind="分页.aspx.cs"...
前台:
复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codebehind="分页.aspx.cs" inherits="分页练习.分页" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>
<asp:textbox id="txtkey" runat="server"></asp:textbox>
<asp:imagebutton id="btnquery" runat="server" onclick="btnquery_click" imageurl="~/images/0.jpg" width="20" height="20" />
<asp:label id="label1" runat="server" text=""></asp:label>
</td>
</tr>
<tr><td><div id="divresult" runat="server"></div></td></tr>
<tr><td>
<asp:linkbutton id="btnfirst" runat="server" onclick="btnfirst_click">第一页</asp:linkbutton>
<asp:linkbutton id="btnbefore" runat="server" onclick="btnbefore_click">上一页</asp:linkbutton>
<asp:linkbutton id="btnnext" runat="server" onclick="btnnext_click">下一页</asp:linkbutton>
<asp:linkbutton id="btnlast" runat="server" onclick="btnlast_click">最后一页</asp:linkbutton>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台:
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.data.sqlclient;
using system.data;
using system.text;
namespace 分页练习
{
public partial class 分页 : system.web.ui.page
{
int pagesize = 3;
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
//viewstate虽然是声明在函数内部,看似是局部变量,但是在类中的其他函数中也可以直接使用
viewstate["pageindex"] = 1;
loaddata();
count();
}
}
//搜索查询
private void loaddata()
{
string strcon = "data source=pc-dll;initial catalog=news;persist security info=true;user id=sa;password=linlin";
sqlconnection conn = new sqlconnection(strcon);
sqlcommand cmd = new sqlcommand();
cmd.connection = conn;
cmd.commandtext = "select top(@pagesize) * from t_news where(newstitle like @newskey or newscontent like @newskey) and id not in(select top ((@pageindex-1)*@pagesize) id from t_news where newstitle like @newskey or newscontent like @newskey order by id )order by id";
cmd.parameters.addwithvalue("@newskey", "%" + txtkey.text + "%");
cmd.parameters.addwithvalue("@pagesize",pagesize);
cmd.parameters.addwithvalue("@pageindex", convert.toint32(viewstate["pageindex"]));
sqldataadapter adapter = new sqldataadapter(cmd);
datatable dt = new datatable();
adapter.fill(dt);
stringbuilder sb1 = new stringbuilder();
sb1.append("<table>");
sb1.append("<tr><td>标题</td><td>内容</td><td>创建时间</td></tr>");
foreach (datarow row in dt.rows)
{
sb1.append("<tr>");
sb1.append("<td>" + row["newstitle"].tostring() + "</td>");
sb1.append("<td>" + row["newscontent"].tostring() + "</td>");
sb1.append("<td>" + row["createtime"].tostring() + "</td>");
sb1.append("</tr>");
}
sb1.append("</table>");
divresult.innerhtml = sb1.tostring();
}
private void count()
{
string strcon = "data source=pc-dll;initial catalog=news;persist security info=true;user id=sa;password=linlin";
sqlconnection conn = new sqlconnection(strcon);
sqlcommand cmd = new sqlcommand();
cmd.connection = conn;
cmd.commandtext = "select count(*) from t_news where newstitle like @newskey or newscontent like @newskey";
cmd.parameters.addwithvalue("@newskey", "%" + txtkey.text + "%");
conn.open();
int totalcount = convert.toint32(cmd.executescalar());
if (totalcount % pagesize == 0)
{
viewstate["pagelastindex"] = totalcount / pagesize;
}
else
{
viewstate["pagelastindex"] = totalcount / pagesize + 1;
}
cmd.dispose();
conn.dispose();
}
//第一页
protected void btnfirst_click(object sender, eventargs e)
{
viewstate["pageindex"] = 1;
loaddata();
}
//上一页
protected void btnbefore_click(object sender, eventargs e)
{
int pageindex = convert.toint32(viewstate["pageindex"]);
if (pageindex > 1)
{
pageindex--;
viewstate["pageindex"] = pageindex;
loaddata();
}
}
//下一页
protected void btnnext_click(object sender, eventargs e)
{
int pageindex = convert.toint32(viewstate["pageindex"]);
if (pageindex < convert.toint32(viewstate["pagelastindex"]))
{
pageindex++;
viewstate["pageindex"] = pageindex;
loaddata();
}
}
//最后一页
protected void btnlast_click(object sender, eventargs e)
{
viewstate["pageindex"] = viewstate["pagelastindex"];
loaddata();
}
protected void btnquery_click(object sender, imageclickeventargs e)
{
count();
loaddata();
}
}
}
推荐阅读
-
.net搜索查询并实现分页实例
-
asp.net中gridview的查询、分页、编辑更新、删除的实例代码
-
asp.net中利用Jquery+Ajax+Json实现无刷新分页的实例代码
-
java web实现分页查询实例方法
-
asp.net中利用Jquery+Ajax+Json实现无刷新分页的实例代码
-
asp.net实现简单分页实例
-
springboot +mybatis 使用PageHelper实现分页并带条件模糊查询功能
-
springboot +mybatis 使用PageHelper实现分页并带条件模糊查询功能
-
asp.net实现简单分页实例
-
EasyUi+Spring Data 实现按条件分页查询的实例代码