. Net search and query and implement paging instances

Source: Internet
Author: User

Front-end:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Paging. aspx. cs" Inherits = "Paging exercise. Paging" %>
<! 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"> page 1 </asp: LinkButton>
<Asp: LinkButton ID = "btnBefore" runat = "server" onclick = "btnBefore_Click"> previous page </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>
</Html>

Background:

Copy codeThe Code is 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 paging exercise
{
Public partial class Page: System. Web. UI. Page
{
Int pagesize = 3;
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
// Although ViewState is declared inside the function and looks like a local variable, it can be directly used in other functions of the class.
ViewState ["pageindex"] = 1;
LoadData ();
Count ();
}
}
// Search and 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) * 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> 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 (*) 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 ();
}
// Page 1
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 ();
}
}
}

Related Article

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.