Extend the pagination function of the gridview

Source: Internet
Author: User
Using system;
Using system. Collections. Generic;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. text;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. reflection;
Using system. reflection. emit;
Using Microsoft. applicationblocks. Data;

Namespace jdglwebcontrols
{
[Defaultproperty ("selectedvalue")]
[Toolboxdata ("<{0}: supergridview runat = Server> </{0}: supergridview>")]
Public class supergridview: gridview
{
# Region "declaring variables"
Private linkbutton lkfirst;
Private linkbutton lkprev;
Private linkbutton lknext;
Private linkbutton lklast;
Private const string pagerinformation = "Total {0} items total {1} pages current {2} pages per page {3 ";
Private const string pagerinformation2 = "Total {1} current {2} pages per page {3 ";
Private const string not_record_found = "<Div align = center> <B style = color: red;> <br> no record </B> <! -- Design by genson Email: genson_diy # Sina.com --> </div> ";
# Endregion

# Region "Custom Attributes"

/// <Summary>
/// Total number of records
/// </Summary>
[Category ("Custom Attributes")]
[Defaultvalue (-1)]
[Description ("Total number of custom records")]
[Browsable (true)]
Public Virtual int recordcount
{
Get
{
Return viewstate ["recordcount"] = NULL? -1: Convert. toint32 (viewstate ["recordcount"]);
}
Set
{
Viewstate ["recordcount"] = value;
}
}

/// <Summary>
/// The text displayed when the supergridview data is empty
/// </Summary>
Public String emptytext
{
Get
{
Return viewstate ["emptytext"] = NULL? Not_record_found: viewstate ["emptytext"]. tostring ();
}
Set
{
Viewstate ["emptytext"] = value;
}
}

# Endregion

# Region "override method"
Protected override void initializepager (gridviewrow row, int columnspan, pageddatasource)
{

Initializedropdownlist (row, columnspan, pageddatasource );
Createpager (row. cells [0]. Controls );
Getrecordcount ();
Createpagerinformation (row. cells [0]. Controls );

// Base. initializepager (row, columnspan, pageddatasource );
}

Public override object datasource
{
Get
{
Return base. datasource;
}
Set
{
Base. datasource = value;
If (value! = NULL)
{
If (datasource is dataset)
Recordcount = (Dataset) datasource). Tables [0]. Rows. count;
If (datasource is icollection)
Recordcount = (icollection) datasource). count;
If (datasource is datatable)
Recordcount = (datatable) datasource). Rows. count;
If (datasource is dataview)
Recordcount = (dataview) datasource). Table. Rows. count;
}

}
}

Protected override void render (htmltextwriter writer)
{
If (httpcontext. Current! = NULL)
{
If (rows. Count = 0)
Writer. Write (not_record_found );
Else
Base. Render (writer );
}
Else
{
Base. Render (writer );
}
}

# Endregion

# Region "custom method"

# Region "dropdownlist used for page initialization"
Void initializedropdownlist (gridviewrow row, int columnspan, pageddatasource)
{
Dropdownlist DRP = new dropdownlist ();
DRP. autopostback = true;
DRP. selectedindexchanged + = new eventhandler (drp_selectedindexchanged );
Literalcontrol LIC = new literalcontrol ("go ");
For (INT I = 0; I <pageddatasource. pagecount; I ++)
{
Listitem li = new listitem (I + 1). tostring () + "page", I. tostring ());
If (pageindex = I)
Li. Selected = true;
DRP. Items. Add (LI );
}
Tablecell cell = new tablecell ();
Cell. Controls. Add (LIC );
Cell. Controls. Add (DRP );
Cell. columnspan = columnspan;
Row. cells. Add (cell );
}

# Endregion

# Endregion

# Region "Create a paging control"

// Create a paging Control
Public void createpager (controlcollection C)
{

C. Add (New literalcontrol ("& nbsp ;"));
Lkfirst = new linkbutton ();
Lkfirst. ID = "lkfirst ";
Lkfirst. commandname = "page ";
Lkfirst. commandargument = "first ";
Lkfirst. Enabled = true;

Lkfirst. Font. Name = "webdings ";
Lkfirst. Font. size = fontunit. Small;
Lkfirst. forecolor = This. forecolor;
Lkfirst. tooltip = "go to the first page ";
Lkfirst. Text = "7 ";

C. Add (lkfirst );
C. Add (New literalcontrol ("& nbsp ;"));

Lkprev = new linkbutton ();
Lkprev. ID = "lkprev ";
Lkprev. commandname = "page ";
Lkprev. commandargument = "Prev ";
Lkprev. Font. Name = "webdings ";
Lkprev. Font. size = fontunit. Small;
Lkprev. forecolor = This. forecolor;
Lkprev. tooltip = "go to the previous page ";
Lkprev. Text = "3 ";
Lkprev. Enabled = true;
If (this. pageindex = 0) lkprev. Enabled = false;

C. Add (lkprev );
C. Add (New literalcontrol ("& nbsp ;"));

Lknext = new linkbutton ();
Lknext. ID = "lknext ";
Lknext. commandname = "page ";
Lknext. commandargument = "Next ";
Lknext. Font. Name = "webdings ";
Lknext. Font. size = fontunit. Small;
Lknext. forecolor = This. forecolor;
Lknext. tooltip = "go to next page ";
Lknext. Text = "4 ";
Lknext. Enabled = true;
If (pageindex = pagecount-1) lknext. Enabled = false;

C. Add (lknext );
C. Add (New literalcontrol ("& nbsp ;"));

Lklast = new linkbutton ();
Lklast. ID = "lklast ";
Lklast. commandname = "page ";
Lklast. commandargument = "last ";
Lklast. Font. Name = "webdings ";
Lklast. Font. size = fontunit. Small;
Lklast. forecolor = This. forecolor;
Lklast. tooltip = "go to the last page ";
Lklast. Text = "8 ";

C. Add (lklast );
C. Add (New literalcontrol ("& nbsp ;"));

}

# Endregion

# Region ""
Public void gotopage (INT pageindex)
{
Gridviewpageeventargs E = new gridviewpageeventargs (pageindex );
This. pageindex = pageindex;
Onpageindexchanging (E );

}

Private void drp_selectedindexchanged (Object sender, eventargs E)
{
Dropdownlist drppager = (dropdownlist) sender;

Gotopage (Int. parse (drppager. selectedvalue ));
}

# Endregion

# Region "create paging information"

// Format: 13 in total, 1 page, current 1 page
Public void createpagerinformation (controlcollection C)
{
Literalcontrol lc0 = new literalcontrol ("");
C. Add (lc0 );
Literalcontrol lc = new literalcontrol ();
If (recordcount! =-1)
{
LC. Text = string. Format (pagerinformation, recordcount, pagecount, pageindex + 1, pagesize );
}
Else
{
LC. Text = string. Format (pagerinformation2, null, pagecount, pageindex + 1, pagesize );
}

C. Add (LC );
}

# Endregion

# Region "reflection"

Private void getrecordcount ()
{
If (! Allowpaging) return;
If (! String. isnullorempty (performanceid ))
{
Try
{
Control C = This. namingcontainer;
Control datasource = C. findcontrol (performanceid );
If (datasource is performancecontrol)
{
Performancecontrol DSC = datasource as performancecontrol;
If (DSC is sqldatasource)
{
Sqldatasource SDS = DSC as sqldatasource;
Commandtype type = commandtype. storedprocedure;
If (SDS. selectcommandtype = sqlperformancecommandtype. Text)
{
Type = commandtype. text;
}
Datatable dt = sqlhelper. executedataset (SDS. connectionstring, type, SDS. selectcommand). Tables [0];
Recordcount = DT. Rows. count;
}

If (DSC is objectdatasource)
{
// Objectdatasource ODS = DSC as objectdatasource;
// If (ODS. enablepaging)
//{
// Throw new notsupportedexception ("Super does not support pagination with the objectdatasource attribute enablepaging = true ");
//}
// Else
//{
// Type typebll = type. GetType (ODS. typename );
// Typebll. invokemember (ODS. selectmethod, bindingflags. declaredonly | bindingflags. Static
// | Bindingflags. invokemethod | bindingflags. Public, null, null, new object [] {string. Empty });
/// Methodinfo method = typebll. getmethod (ODS. selectmethod );
//}
}
}
}
Catch (exception ex)
{
// Throw ex;
}
}
}
# Endregion
}
}

Related Article

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.