.net搜索查詢并實(shí)現(xiàn)分頁(yè)實(shí)例
更新時(shí)間:2013年03月14日 10:35:14 作者:
.net搜索查詢并實(shí)現(xiàn)分頁(yè)實(shí)例,需要的朋友可以參考一下
前臺(tái):
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="分頁(yè).aspx.cs" Inherits="分頁(yè)練習(xí).分頁(yè)" %>
<!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">第一頁(yè)</asp:LinkButton>
<asp:LinkButton ID="btnBefore" runat="server" onclick="btnBefore_Click">上一頁(yè)</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" onclick="btnNext_Click">下一頁(yè)</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" onclick="btnLast_Click">最后一頁(yè)</asp:LinkButton>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后臺(tái):
復(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 分頁(yè)練習(xí)
{
public partial class 分頁(yè) : 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>標(biāo)題</td><td>內(nèi)容</td><td>創(chuàng)建時(shí)間</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();
}
//第一頁(yè)
protected void btnFirst_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = 1;
LoadData();
}
//上一頁(yè)
protected void btnBefore_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32(ViewState["pageindex"]);
if (pageindex > 1)
{
pageindex--;
ViewState["pageindex"] = pageindex;
LoadData();
}
}
//下一頁(yè)
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();
}
}
//最后一頁(yè)
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)文章
ASP.NET Ref和Out關(guān)鍵字區(qū)別分析
類型介紹 在幾乎所有的OOP語(yǔ)言中,都存在2種類型的值。2009-02-02asp.net GridView控件鼠標(biāo)移動(dòng)某行改變背景顏色(方法一)
asp.net GridView控件鼠標(biāo)移動(dòng)某行改變背景顏色2009-12-12靜態(tài)gb2312編碼在項(xiàng)目傳值出現(xiàn)中文亂碼現(xiàn)象
參考的美工靜態(tài)頁(yè)面是gb2312格式的,當(dāng)此編碼拿到項(xiàng)目中后,utf-8編碼的系統(tǒng),加載頁(yè)面時(shí),會(huì)出現(xiàn)樣式問題,比如不能正常居中等2013-06-06asp.net生成字母和數(shù)字混合圖形驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了asp.net生成字母和數(shù)字混合圖形驗(yàn)證碼的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02ASP.NET對(duì)路徑"xxxxx"的訪問被拒絕的解決方法小結(jié)
異常詳細(xì)信息: System.UnauthorizedAccessException: 對(duì)路徑“D:/temp1/MyTest.txt”的訪問被拒絕2012-09-09ASP.NET中 Execl導(dǎo)出的六種方法實(shí)例
這篇文章主要介紹了ASP.NET中 Execl導(dǎo)出的六種方法實(shí)例,有需要的朋友可以參考一下2013-12-12