欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【.net搜索查询并实现分页实例】,下面是详细的讲解!
.net搜索查询并实现分页实例
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搜索查询并实现分页实例的用户互动如下:
相关问题:请问excel怎样实现分页查询,请高手指点。。附:ht...
答:如何实现excel模糊查找,请高手指教 ab 宝华商行1 宝华商行12 宝华商行23 宝华商行34 宝华商行45 宝华商行56 宝华商行67 名称 宝华=INDEX($A$3:$A$9,MATCH(Criteria,$A$3:A3,-1))=INDEX($B$3:$B$9,MATCH(Criteria,$A$3:A3,-1)) =INDEX($A$3:$A$9... >>详细
相关问题:php与ajax的搜索分页实现如何实现?
答:php是用来和服务器(同步)交互的环境平台,单纯用PHP开发网站也没有任何问题,但是没有任何智能化可言,因为每个动作都需要请求服务器(刷新页面), ajax(Asynchronous JavaScript and XML)也是与服务器(异步)交互模式 javascript是一种客户端语言,它... >>详细
相关问题:asp.net如何实现gridview控件进行分页并带有查询的...
答:.aspx文件中的代码(格式你自己调): 首页 上一页 下一页 尾页 跳转到第 页 .aspx.cs文件中的代码: protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { this.GridView1.PageIndex = this.ddlCurrentPage.Selec... >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【404页面】ASP.NET设置404页面返回302HTTP状态码
- 【Visual】分享Visual Studio原生开发的10个调试
- 【全局】.net全局定时定期执行某些操作在Global.a
- 【asp】asp.net ubb使用代码-net-ubb使用
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
