Paging Control source code--composite Control

Source: Internet
Author: User
Tags sort table name tostring
Pagination | control | source code 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
{
?///
?///Coolpager's summary description.
?///
? public enum SortType
? {
?? Asc=0,desc=1
?};
? public enum Iscount
? {
?? Noreturnrecordcount=0,returnrecordcount
?};
? [Defaultproperty ("CopyRight"),
?? ToolBoxData ("<{0}:coolpager runat=server>")]
? 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: A 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 to display pagination"), 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 Name
?? [Description ("Sort Fields"), DefaultValue ("")]
?? public string FieldName
?? {
??? Get
??? {
???? Object obj=viewstate["FieldName"];
???? Return (obj==null)? String.Empty:obj. ToString ();
???}
??? Set
??? {
???? viewstate["FieldName"]=value;
???}
??}
?? Show number of records 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
?? [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 ("returns Total Records"), DefaultValue ("")]
?? Public Iscount Iscount
?? {
??? Get
??? {
???? return iscount.returnrecordcount;
???}
??}
?? Sort Type
?? [Description ("Sort Type"), DefaultValue ("")]
?? Public SortType 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 ("Show number of 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 pages, automatically generated"), DefaultValue ("")]
?? public int PageCount
?? {
??? Get
??? {
???? Object obj=viewstate["PageCount"];
???? Return (obj==null) 1:convert.toint32 (obj);
???}
??? Set
??? {
???? viewstate["PageCount"]=value;
???}
??}

?? ///
?? Renders this control to the specified output parameter.
?? ///
?? The HTML writer to write to
?? 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 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 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:" +pageindex.tostring () + "|" +pagecount.tostring () + "Record:" + ((pageIndex-1) *pagesize+1) + "-" + (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 (""));
???}
??? //*************************
??? for (int i=0;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 (""));
????}
???}
??? 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 ("");
??? 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= "page number cannot be empty!";
??? 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.