Datalist page (codebehind part)

Source: Internet
Author: User
Datalist page (codebehind part)

Post: Andy. m Date: popularity: 30

Using system;
Using system. collections;
Using system. configuration;
Using system. componentmodel;
Using system. Data;
Using system. Data. sqlclient;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Namespace cj168.web
{
/// <Summary>
/// Summary description for search.
/// </Summary>
Public Class search: system. Web. UI. Page

{
Protected system. Web. UI. webcontrols. datalist datalistsearchresult;
Protected system. Web. UI. webcontrols. Label lbltxtsearch;
Protected system. Web. UI. webcontrols. Label lblrecordcount;
Protected system. Web. UI. webcontrols. Label lblcurrentpage;
Protected system. Web. UI. webcontrols. Label lblpagecount;
Protected system. Web. UI. webcontrols. Label lblpagerange;

Protected system. Web. UI. webcontrols. linkbutton lbnprevpage;
Protected system. Web. UI. webcontrols. linkbutton lbnnextpage;
String txtsearch;
Int pagesize, recordcount, pagecount, currentpage;

Public Search ()
{
Page. init + = new system. eventhandler (page_init );
}

Private void page_load (Object sender, system. eventargs E)
{
// Put user code to initialize the page here
Txtsearch = request. Params ["txtsearch"];
If (txtsearch! = NULL)
{Txtsearch = txtsearch. Replace ("'","");

Lbltxtsearch. Text = txtsearch;

// Set pagesize
Pagesize = 10;

// Calculate the total number of records
Recordcount = calculaterecord ();
Lblrecordcount. Text = recordcount. tostring ();

If (! Page. ispostback)

{

Dosearch ();
Currentpage = 0;
Viewstate ["pageindex"] = 0;

// Calculate the total number of pages
Pagecount = recordcount/pagesize;
If (recordcount % pagesize> 0)
Pagecount = pagecount + 1;
Lblpagecount. Text = pagecount. tostring ();
Viewstate ["pagecount"] = pagecount;


}
}

}

// Calculate the total number of records
Public int calculaterecord ()
{
Cj168.dataaccess. magsdb dosearch = new cj168.dataaccess. magsdb ();
Return dosearch. getsearchresultcount (txtsearch );

}

Void dosearch ()
{
Int startindex;
Int fromitem, toitem;

// Set the import start and end addresses
Startindex = currentpage * pagesize;

Cj168.dataaccess. magsdb dosearch = new cj168.dataaccess. magsdb ();
Datalistsearchresult. datasource = dosearch. getsearchresult (txtsearch, startindex, pagesize );
Datalistsearchresult. databind ();

Lbnnextpage. Enabled = true;
Lbnprevpage. Enabled = true;
If (currentpage = (PageCount-1) lbnnextpage. Enabled = false;
If (currentpage = 0) lbnprevpage. Enabled = false;
Lblcurrentpage. Text = (currentpage + 1). tostring ();

Fromitem = startindex + 1;
Toitem = startindex + pagesize;

If (recordcount <toitem)
Toitem = recordcount;

Lblpagerange. Text = fromitem + "-" + toitem;

}

Public void page_onclick (Object sender, commandeventargs E)
{
Currentpage = (INT) viewstate ["pageindex"];
Pagecount = (INT) viewstate ["pagecount"];

String cmd = E. commandname;
// Identify cmd to determine the page turning direction
Switch (CMD)
{
Case "Next ":
If (currentpage (PageCount-1) currentpage ++;
Break;
Case "Prev ":
If (currentpage> 0) currentpage --;
Break;
}

Viewstate ["pageindex"] = currentpage;

Dosearch ();


}



Private void page_init (Object sender, eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
}

# Region web form designer generated code
/// <Summary>
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion
}
}

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.