.net搜索查詢并實現(xiàn)分頁實例
更新時間:2013年03月14日 10:35:14 作者:
.net搜索查詢并實現(xiàn)分頁實例,需要的朋友可以參考一下
前臺:
復(fù)制代碼 代碼如下:
<%@ 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>
后臺:
復(fù)制代碼 代碼如下:
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雖然是聲明在函數(shù)內(nèi)部,看似是局部變量,但是在類中的其他函數(shù)中也可以直接使用
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>內(nèi)容</td><td>創(chuàng)建時間</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();
}
}
}
相關(guān)文章
C#調(diào)用C++版本dll時的類型轉(zhuǎn)換需要注意的問題小結(jié)
最近使用C#調(diào)用C++版本的dll遇到很多類型轉(zhuǎn)換的問題,現(xiàn)記錄出容易出錯的部分。2010-04-04
ASP.NET Mvc開發(fā)之查詢數(shù)據(jù)
這篇文章主要介紹了ASP.NET Mvc開發(fā)之查詢數(shù)據(jù)的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-02-02
使用JavaScript代碼實現(xiàn)各種數(shù)據(jù)控件的反選功能 不要只做拖控件的菜鳥
在我們做許多項目的時候,會用到反選這個功能,但是我一般使用C#代碼創(chuàng)建數(shù)組遍歷實現(xiàn)功能,今天我想換一種語言實現(xiàn)一下,于是我就用JavaScript研究了一下怎么實現(xiàn)這個功能2011-12-12
asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼實例
這篇文章主要介紹了asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼,結(jié)合實例形式詳細分析了asp.net基于QRCode生成二維碼的具體實現(xiàn)技巧,需要的朋友可以參考下2016-06-06

