Customize the pagination function of the gridview Control

Source: Internet
Author: User

The following is the key code of the paging control. The disadvantage of this Code is that the query statement is limited.

Private datatable binddatatable (INT pagenum)
{
Sqlstring = "select corpnum, nickname, corpname, ownername, address, phonenum, username, modifytime, corpcount, carcount, drivercount from tbconfigurationinfo order by modifytime ";
This. labelpagenum. Text = pagenum. tostring ();
Int page_num = convert. toint32 (this. labelpagenum. Text );
Page_counttotal = sqlhelp. getpagecount (sqlstring );
Datatable = sqlhelp. getdatatable (sqlstring, page_num, pagesize, page_counttotal );
Int pagecount = getpagecount (page_counttotal );
This. labelpagecount. Text = pagecount. tostring ();
Return datatable;
}
Private int getpagecount (INT page_count)
{

Int num = 0;
Int totalnum = page_count;
If (totalnum % pagesize = 0)
{
Num = totalnum/pagesize;
}
Else
{
Num = totalnum/pagesize + 1;
}
Return num;
}
Protected void btnnext_click (Object sender, eventargs E)
{
Txtboxturn. Text = "";
Int num = convert. toint32 (this. labelpagenum. Text) + 1;
If (Num <= convert. toint32 (this. labelpagecount. Text ))
{
Datatable dt = binddatatable (Num );
If (DT! = NULL & DT. Rows. Count> 0)
{
This. gridview1.datasource = DT;
This. gridview1.databind ();
}
}
}
Protected void btnturn_click (Object sender, eventargs E)
{
Int num = convert.toint32(this.txt boxturn. Text );
If (Num <= convert. toint32 (this. labelpagecount. Text ))
{
Datatable dt = binddatatable (Num );
If (DT! = NULL & DT. Rows. Count> 0)
{
This. gridview1.datasource = DT;
This. gridview1.databind ();
}
}
}
Protected void btnpre_click (Object sender, eventargs E)
{
Txtboxturn. Text = "";
Int num = convert. toint32 (this. labelpagenum. Text)-1;
If (Num> = 1)
{
Datatable dt = binddatatable (Num );
If (DT! = NULL & DT. Rows. Count> 0)
{
This. gridview1.datasource = DT;
This. gridview1.databind ();
}
}
}
Protected void btnfirst_click (Object sender, eventargs E)
{
Txtboxturn. Text = "";
Int num = 1;
Datatable dt = binddatatable (Num );
If (DT! = NULL & DT. Rows. Count> 0)
{
This. gridview1.datasource = DT;
This. gridview1.databind ();
}

}
Protected void btnlast_click (Object sender, eventargs E)
{
Txtboxturn. Text = "";
Int num = convert. toint32 (this. labelpagecount. Text );
Datatable dt = binddatatable (Num );
If (DT! = NULL & DT. Rows. Count> 0)
{
This. gridview1.datasource = DT;
This. gridview1.databind ();
}
}

 

 

Public static datatable getdatatable (string STR, int pagenum, int pagesize, int page_totalcount)
{
String page_numfrom = "";
If (pagenum> (page_totalcount/pagesize ))
{
Int I = page_totalcount % pagesize;
Page_numfrom = I. tostring ();
}
Else
{
Page_numfrom = pagesize. tostring ();
}
Int page_numto = pagenum * pagesize;
String page_numto = page_numto.tostring ();
String str_select = Str. Replace ("select ","");
String strreplacefrom = Str. tolower (). Replace ("from ","/");
String [] str_out = strreplacefrom. Split ('/');
String strreplaceorder = Str. tolower (). Replace ("order ","/");
String [] str_order = strreplaceorder. Split ('/');
String str_order_true = str_order [1];
String str_outtrue = str_out [0];
Str_outtrue = str_outtrue.replace ("select ","");
String str_select_out = str_select.replace ("from ","/");
String [] str_select_array = str_select_out.split ('/');
String str_select_first = str_select_array [0];
String str_last = "select top"
+ Page_numfrom + str_select_first
+ "From (" + "select top" + page_numto
+ Str_select + ")" + "as derivedtbl_1 order"
+ Str_order_true + "DESC ";
Dataset DS = getdatset_string (str_last, connstring );
Datatable = Ds. Tables [0];
Return datatable;

}

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.