The basic functions are as follows:
CopyCode The Code is as follows: // <summary>
/// Used when paging is required. The datatable is obtained based on the parameters and conditionexpress.
/// </Summary>
/// <Param name = "_ tablename"> table name </param>
/// <Param name = "_ fieldnames"> Field name set, separated by commas </param>
/// <Param name = "_ ordercolumn"> the sorting field used to count the number of records. </param>
/// <Param name = "isdesc"> Reverse Order </param>
/// <Param name = "_ indexcolumn"> auto-increment field name </param>
/// <Param name = "_ currentpage"> current page </param>
/// <Param name = "pagesize"> page size </param>
/// <Param name = "_ rowscount"> total number of records </param>
/// <Returns> the retrieved able </returns>
Public static datatable getdatatable (string _ tablename, string _ fieldnames, string _ ordercolumn, bool isdesc, string _ indexcolumn, int _ currentpage, int pagesize, string conditionexpress, ref int _ rowscount)
{
Using (sqlconnection conn = new sqlconnection (connectionstring ))
{
String wherestr = "where 1 = 1 ";
String sort = isdesc? "DESC": "ASC ";
String sqlstr = "from" + _ tablename;
// Sort Fields
String orderstr = "order by" + _ ordercolumn + sort;
If (_ ordercolumn! = _ Indexcolumn)
Orderstr + = "," + _ indexcolumn + sort;
If (conditionexpress! = String. Empty)
{
Wherestr + = conditionexpress;
}
Sqlstr + = wherestr;
// Obtain the total number of Qualified Data
Sqlcommand cmd = new sqlcommand ("select count (" + _ ordercolumn + ")" + sqlstr, Conn );
Conn. open ();
Try
{
_ Rowscount = (INT) cmd. executescalar ();
}
Catch (exception ex)
{
Throw new exception (ex. Message );
}
If (_ currentpage> _ rowscount) _ currentpage = _ rowscount;
If (_ currentpage> 1)
{
If (isdesc)
Sqlstr + = "and" + _ ordercolumn + "<(select Min (" + _ ordercolumn + ") from ";
Else
Sqlstr + = "and" + _ ordercolumn + "> (select max (" + _ ordercolumn + ") from ";
Sqlstr + = "(select top" + (pagesize * (_ currentpage-1) + "" + _ ordercolumn + "from" + _ tablename + wherestr + orderstr + ") as T )";
}
Sqlstr = "select top" + pagesize + "" + _ fieldnames + sqlstr + orderstr;
Try
{
Dataset DS = new dataset ();
Sqldataadapter da = new sqldataadapter (sqlstr, Conn );
Da. Fill (DS );
Return Ds. Tables [0];
}
Catch (exception ex)
{
Throw new exception (ex. Message );
}
}
}
The call is as follows:Copy codeThe Code is as follows: Private void BIND ()
{
Int rowcount = 1;
String wherestr = string. empty;
// Set pagination
Anpager. alwaysshow = true;
Anpager. pagesize = 10;
This. rptdictionary. datasource = getdatatable (
"Dictionary_toysgogo _",
"[Id_dictionary _], [namecn_dictionary _], [nameen_dictionary _], [point_dictionary _]",
"[Id_dictionary _]",
True,
"[Id_dictionary _]",
This. anpager. currentpageindex,
Anpager. pagesize,
Wherestr,
Ref rowcount
);
This. anpager. recordcount = rowcount;
This. rptdictionary. databind ();
}
Copy codeThe Code is as follows: // page Switch
Protected void anpager_pagechanging (Object SRC, Wuqi. webdiyer. pagechangingeventargs E)
{
This. anpager. currentpageindex = E. newpageindex;
This. tbxtype. Text = This. tbxtype. text;
BIND ();
}
I used to write numbers directly to the page number, but not anpager. in the form of pagesize = 10; with the reminder of laotang, the modification has also solved the problem that has always plagued me.