Paging control source code -- Composite Control

Source: Internet
Author: User

Using system;
Using system. IO;
Using system. drawing;
Using system. Data;
Using system. Data. sqlclient;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. componentmodel;

Namespace Pager
{
/// <Summary>
/// Summary of coolpager.
/// </Summary>
Public Enum sorttype
{
ASC = 0, DESC = 1
};
Public Enum iscount
{
Noreturnrecordcount = 0, returnrecordcount
};
[Defaultproperty ("Copyright "),
Toolboxdata ("<{0}: coolpager runat = Server> </{0}: coolpager>")]
Public class coolpager: system. Web. UI. webcontrols. webcontrol, inamingcontainer
{
Protected int recordcount = 0;
Protected control _ controltopaginate;
Protected pageddatasource _ datasource;

[Bindable (true ),
Category ("appearance "),
Defaultvalue ("")]
Public String Copyright
{
Get
{
Return "All Rights Reserved: one bird in Qianshan ";
}
}
// Connection string
[Description ("data source connection string"), defaultvalue ("")]
Public String connectionstring
{
Get
{
Object OBJ = viewstate ["connectionstring"];
Return (OBJ = NULL )? String. Empty: obj. tostring ();
}
Set
{
Viewstate ["connectionstring"] = value;
}
}
// Controls that display pagination
[Description ("controls used to display pages"), defaultvalue ("")]
Public String controltopaginate
{
Get
{
Object OBJ = viewstate ["controltopaginate"];
Return (OBJ = NULL )? String. Empty: obj. tostring ();
}
Set
{
Viewstate ["controltopaginate"] = value;
}
}
// Table name
[Description ("query table name"), defaultvalue ("")]
Public String tablename
{
Get
{
Object OBJ = viewstate ["tablename"];
Return (OBJ = NULL )? String. Empty: obj. tostring ();
}
Set
{
Viewstate ["tablename"] = value;
}
}
// Sort Field Names
[Description ("sorting field"), defaultvalue ("")]
Public String fieldname
{
Get
{
Object OBJ = viewstate ["fieldname"];
Return (OBJ = NULL )? String. Empty: obj. tostring ();
}
Set
{
Viewstate ["fieldname"] = value;
}
}
// Number of records displayed per page
[Description ("number of records per page"), defaultvalue ("")]
Public int pagesize
{
Get
{
Object OBJ = viewstate ["pagesize"];
Return (OBJ = NULL )? 15: Convert. toint32 (OBJ );
}
Set
{
Viewstate ["pagesize"] = value;
}
}
// Page number
[Description ("Current page number"), defaultvalue ("")]
Public int pageindex
{
Get
{
Object OBJ = viewstate ["pageindex"];
Return (OBJ = NULL )? 1: Convert. toint32 (OBJ );
}
Set
{
Viewstate ["pageindex"] = value;
}
}
// Whether to return the total number of records
[Description ("whether to return the total number of records"), defaultvalue ("")]
Public iscount
{
Get
{
Return iscount. returnrecordcount;
}
}
// Sorting type
[Description ("sorting type"), defaultvalue ("")]
Public sorttype
{
Get
{
Object OBJ = viewstate ["sorttype"];
Return (OBJ = NULL )? Sorttype. ASC: (sorttype) OBJ;
}
Set
{
Viewstate ["sorttype"] = value;
}
}
// Where clause
[Description ("WHERE clause, without where"), defaultvalue ("")]
Public String where
{
Get
{
Object OBJ = viewstate ["where"];
Return (OBJ = NULL )? String. Empty: obj. tostring ();
}
Set
{
Viewstate ["where"] = value;
}
}
[Description ("Number of displayed navigation numbers"), defaultvalue ("")]
Public int showpagenumber
{
Get
{
Object OBJ = viewstate ["showpagenumber"];
Return (OBJ = NULL )? 10: Convert. toint32 (OBJ );
}
Set
{
Viewstate ["showpagenumber"] = value;
}
}
[Description ("start page"), defaultvalue ("")]
Public int pagestart
{
Get
{
Object OBJ = viewstate ["pagestart"];
Return (OBJ = NULL )? 1: Convert. toint32 (OBJ );
}
Set
{
Viewstate ["pagestart"] = value;
}
}
[Description ("total page number, automatically generated"), defaultvalue ("")]
Public int pagecount
{
Get
{
Object OBJ = viewstate ["pagecount"];
Return (OBJ = NULL )? 1: Convert. toint32 (OBJ );
}
Set
{
Viewstate ["pagecount"] = value;
}
}

/// <Summary>
/// Present the control to the specified output parameter.
/// </Summary>
/// <Param name = "output"> HTML writer to be written </param>
Protected override void render (htmltextwriter output)
{
If (page. enableviewstate = false)
Page. enableviewstate = true;
If (site! = NULL & site. designMode)
If (! Page. ispostback)
Createchildcontrols ();
Base. Render (output );
}
Public override void databind ()
{
Base. databind ();

Childcontrolscreated = false;

If (controltopaginate = "")
Return;
_ Controltopaginate = page. findcontrol (controltopaginate );
If (_ controltopaginate = NULL)
Return;
If (! (_ Controltopaginate is basedatalist | _ controltopaginate is listcontrol ))
Return;

If (connectionstring = "" | tablename = "")
Return;

// Fetch data
Fetchdata ();

// Bind data to the buddy Control
Basedatalist basedatalistcontrol = NULL;
Listcontrol = NULL;
If (_ controltopaginate is basedatalist)
{
Basedatalistcontrol = (basedatalist) _ controltopaginate;
Basedatalistcontrol. datasource = _ datasource;
Basedatalistcontrol. databind ();
Return;
}
If (_ controltopaginate is listcontrol)
{
Listcontrol = (listcontrol) _ controltopaginate;
Listcontrol. Items. Clear ();
Listcontrol. datasource = _ datasource;
Listcontrol. databind ();
Return;
}
}
Private void fetchdata ()
{
Sqlconnection Cn = new sqlconnection (connectionstring );
Sqlcommand CM = new sqlcommand ();
Cm. Connection = cn;
Cm. Connection. open ();

Cm. commandtext = "getrecordfrompage ";
Cm. commandtype = commandtype. storedprocedure;
Cm. Parameters. Add ("@ tblname", sqldbtype. nvarchar, 255 );
Cm. Parameters. Add ("@ fldname", sqldbtype. nvarchar, 255 );
Cm. Parameters. Add ("@ pagesize", sqldbtype. INT );
Cm. Parameters. Add ("@ pageindex", sqldbtype. INT );
Cm. Parameters. Add ("@ iscount", sqldbtype. Bit );
Cm. Parameters. Add ("@ ordertype", sqldbtype. Bit );
Cm. Parameters. Add ("@ strwhere", sqldbtype. nvarchar, 1000 );
Cm. Parameters ["@ tblname"]. value = tablename;
Cm. Parameters ["@ fldname"]. value = fieldname;
Cm. Parameters ["@ pagesize"]. value = pagesize;
Cm. Parameters ["@ pageindex"]. value = pageindex;
Cm. Parameters ["@ iscount"]. value = 0;
Cm. Parameters ["@ ordertype"]. value = sorttype;
Cm. Parameters ["@ strwhere"]. value = where;

Sqldataadapter adapter = new sqldataadapter (CM );
Datatable dtb = new datatable ();
Adapter. Fill (dtb );

Cm. Parameters ["@ iscount"]. value = iscount;
Recordcount = (INT) cm. executescalar ();
Pagecount = (INT) math. Ceiling (double) recordcount/pagesize );

Cm. Connection. Close ();
Cm. Dispose ();
CN. Dispose ();
Adapter. Dispose ();

// Configures the paged data source component
If (_ datasource = NULL)
_ Datasource = new pageddatasource ();
_ Datasource. allowcustompaging = true;
_ Datasource. allowpaging = true;
_ Datasource. currentpageindex = 0;
If (pageindex = pagecount)
{
_ Datasource. pagesize = (recordcount-(pageindex-1) * pagesize );
}
Else
{
_ Datasource. pagesize = pagesize;
}
If (pageindex = pagecount)
{
_ Datasource. virtualcount = (recordcount-(pageindex-1) * pagesize );
}
Else
{
_ Datasource. virtualcount = pagesize;
}
_ Datasource. datasource = dtb. defaultview;
}
Protected override void createchildcontrols ()
{
Controls. Clear ();
If (! Page. ispostback)
{
Clearchildviewstate ();
}

//************************************** *********
Table = new table ();
Table. width = unit. percentage (100 );
Table. cellpadding = 0;
Table. cellspacing = 3;
Table. borderwidth = 0;
Table. Font. size = fontunit. Point (9 );

Tablerow TR = new tablerow ();

Tablecell tdinfo = new tablecell ();
Tdinfo. width = unit. percentage (25 );
Tdinfo. horizontalalign = horizontalalign. Left;
Buildinfo (tdinfo );
Tr. cells. Add (tdinfo );

Tablecell tdnavigate = new tablecell ();
Tdnavigate. width = unit. percentage (50 );
Tdnavigate. horizontalalign = horizontalalign. Center;
Buildnavigator (tdnavigate );
Tr. cells. Add (tdnavigate );

Tablecell tdgoto = new tablecell ();
Tdgoto. width = unit. percentage (25 );
Tdgoto. horizontalalign = horizontalalign. Right;
Buildgoto (tdgoto );
Tr. cells. Add (tdgoto );

Table. Rows. Add (TR );
Controls. Add (table );
}
Private void buildinfo (tablecell TD)
{
Literalcontrol lc = new literalcontrol ();
LC. TEXT = "page number:" + pageindex. tostring () + "|" + pagecount. tostring () + "record:" + (pageindex-1) * pagesize + 1) + "-" + (recordcount <pagesize )? Recordcount: (pageindex = pagecount )? Recordcount: pageindex * pagesize) + "|" + recordcount;
TD. Controls. Add (LC );
}
Private void buildnavigator (tablecell TD)
{
Int temp;
If (pageindex> showpagenumber)
{
Linkbutton lbback = new linkbutton ();
Lbback. causesvalidation = false;
Lbback. Text = "...";
Lbback. commandargument = "back ";
Lbback. Command + = new commandeventhandler (lb_click );
TD. Controls. Add (lbback );
TD. Controls. Add (New literalcontrol ("& nbsp ;"));
}
//*************************
For (INT I = 0; I <showpagenumber; I ++)
{
If (pagestart + I) <= pagecount)
{
Temp = pagestart + I;
Linkbutton lbnumber = new linkbutton ();
Lbnumber. causesvalidation = false;
Lbnumber. Text = temp. tostring ();
Lbnumber. commandargument = temp. tostring ();
Lbnumber. Command + = new commandeventhandler (lbnumber_click );
TD. Controls. Add (lbnumber );
TD. Controls. Add (New literalcontrol ("& nbsp ;"));
}
}
If (pagecount-pagestart)> showpagenumber)
{
Linkbutton lbforward = new linkbutton ();
Lbforward. causesvalidation = false;
Lbforward. Text = "...";
Lbforward. commandargument = "Forward ";
Lbforward. Command + = new commandeventhandler (lb_click );
TD. Controls. Add (lbforward );
}
}
Private void buildgoto (tablecell TD)
{
Textbox TB = new Textbox ();
TB. ID = "gotopage ";
TB. borderwidth = unit. pixel (1 );
TB. width = unit. pixel (30 );
TB. enableviewstate = true;
TD. Controls. Add (TB );

Literalcontrol lc = new literalcontrol ("& nbsp ;");
TD. Controls. Add (LC );

Button BTN = new button ();
BTN. Text = "Goto ";
BTN. commandargument = Tb. clientid;
BTN. Command + = new commandeventhandler (lb_click );
TD. Controls. Add (BTN );

Requiredfieldvalidator val2 = new requiredfieldvalidator ();
Val2.controltovalidate = "gotopage ";
Val2.errormessage = "the page number cannot be blank! ";
Val2.display = validatordisplay. None;
Val2.id = "val2 ";
TD. Controls. Add (val2 );

Rangevalidator val1 = new rangevalidator ();
Val1.display = validatordisplay. None;
Val1.errormessage = "invalid page number! ";
Val1.id = "val1 ";
Val1.controltovalidate = "gotopage ";
Val1.type = validationdatatype. Integer;
Val1.minimumvalue = "1 ";
If (pagecount <1)
{
Val1.maximumvalue = "1 ";
}
Else
{
Val1.maximumvalue = pagecount. tostring ();
}

TD. Controls. Add (val1 );

Validationsummary valall = new validationsummary ();
Valall. showmessagebox = true;
Valall. showsummary = false;
TD. Controls. Add (valall );
}
Private void lb_click (Object sender, commandeventargs E)
{
Switch (E. commandargument. tostring ())
{
Case "back ":
Pagestart-= showpagenumber;
Pageindex = pagestart;
Createchildcontrols ();
Break;
Case "Forward ":
Pagestart + = showpagenumber;
Pageindex = pagestart;
Createchildcontrols ();
Break;
Default:
// Textbox TB = (textbox) parent. findcontrol ("coolpager1: gotopage ");
// Pageindex = convert. toint32 (Tb. Text );
String temp = page. Request. Form [This. clientid + ": gotopage"]. tostring (). Trim ();
Try
{
Pageindex = convert. toint32 (temp );
If (pageindex % showpagenumber = 0)
{
Pagestart = pageindex + 1-showpagenumber;
}
Else
{
Pagestart = (INT) math. Floor (double) pageindex/showpagenumber) * showpagenumber + 1;
}
Createchildcontrols ();
}
Catch
{
}
Break;
}
Databind ();
}
Private void lbnumber_click (Object sender, commandeventargs E)
{
Pageindex = convert. toint32 (E. commandargument );
Databind ();
}
}
}

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.