. NET search query and implementation of pagination instances _ practical Tips

Source: Internet
Author: User

Front desk:

Copy Code code as follows:

<%@ page language= "C #" autoeventwireup= true "codebehind=" paging. Aspx.cs "inherits=" pagination exercise. Paging "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<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= "/>"
<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" > First page </asp:LinkButton>
<asp:linkbutton id= "Btnbefore" runat= "Server" onclick= "Btnbefore_click" > Prev </asp:LinkButton>
<asp:linkbutton id= "Btnnext" runat= "Server" onclick= "Btnnext_click" > next page </asp:LinkButton>
<asp:linkbutton id= "Btnlast" runat= "Server" onclick= "Btnlast_click" > last page </asp:LinkButton>
</td>
</tr>
</table>
</div>
</form>
</body>

Background:

Copy Code code as follows:

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 pagination Exercise
{
Public partial class paging: System.Web.UI.Page
{
int pagesize = 3;
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
ViewState although it is declared inside a function, it appears to be a local variable, but it can also be used directly in other functions in the class
viewstate["pageindex"] = 1;
LoadData ();
Count ();
}
}
Search query
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) * T_news WHERE (newstitle like @newskey OR newscontent like @newskey) and Id not In (@pageindex-1) * @pagesize) Id from T_news WHERE newstitle like @newskey OR newscontent like @newskey order B Y 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> title </td><td> content </td><td> creation time </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 (*) 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 ();
}
First page
protected void Btnfirst_click (object sender, EventArgs e)
{
viewstate["pageindex"] = 1;
LoadData ();
}
Previous page
protected void Btnbefore_click (object sender, EventArgs e)
{

int pageindex = Convert.ToInt32 (viewstate["pageindex"]);
if (pageindex > 1)
{
pageindex--;
viewstate["pageindex"] = pageindex;
LoadData ();
}
}
Next page
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 ();
}
}
Last page
protected void Btnlast_click (object sender, EventArgs e)
{
viewstate["pageindex"] = viewstate["Pagelastindex"];
LoadData ();
}
protected void Btnquery_click (object sender, ImageClickEventArgs e)
{
Count ();
LoadData ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.