主頁 > 知識庫 > .net搜索查詢并實現(xiàn)分頁實例

.net搜索查詢并實現(xiàn)分頁實例

熱門標簽:杭州智能電話機器人 百度地圖標注點擊事件 內(nèi)蒙古智能電銷機器人哪家強 地圖標注位置多的錢 怎樣在地圖標注消火栓圖形 廈門四川外呼系統(tǒng) 泰州手機外呼系統(tǒng)軟件 濟源人工智能電話機器人價格 山東防封電銷卡辦理套餐

前臺:

復(fù)制代碼 代碼如下:

%@ Page Language="C#" AutoEventWireup="true" CodeBehind="分頁.aspx.cs" Inherits="分頁練習(xí).分頁" %>
!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 分頁練習(xí)
{
    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();
        }
    }
}

您可能感興趣的文章:
  • ASP.NET MVC5 實現(xiàn)分頁查詢的示例代碼
  • asp.net中g(shù)ridview的查詢、分頁、編輯更新、刪除的實例代碼
  • MVC+EasyUI+三層新聞網(wǎng)站建立 分頁查詢數(shù)據(jù)功能(七)

標簽:洛陽 周口 朔州 朝陽 百色 新鄉(xiāng) 喀什 臺州

巨人網(wǎng)絡(luò)通訊聲明:本文標題《.net搜索查詢并實現(xiàn)分頁實例》,本文關(guān)鍵詞  .net,搜索,查詢,并,實現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《.net搜索查詢并實現(xiàn)分頁實例》相關(guān)的同類信息!
  • 本頁收集關(guān)于.net搜索查詢并實現(xiàn)分頁實例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章