A DataGrid paging control written in C # And called in VB.net will be continuously improved.

Source: Internet
Author: User

using system;
using system. Web. UI;
using system. Web. UI. webcontrols;
using system. componentmodel;

namespace wxc
{< br> ///


// prepared: kasafuma
//
[defaultproperty ("text"),
toolboxdata ("<{0 }: pager runat = Server> ")]
public class Pager: system. web. UI. control, inamingcontainer
{< br> [description ("Select page event")]
public event eventhandler pagechanged;
# region control property list
[browsable (false),
description ("Total number of records on all pages of the dataset "),
category ("pagination"),
Defaultvalue (0)]
Public int recordcount
{< br> Get
{< br> Object OBJ = viewstate ["recordcount"];
return (OBJ = NULL )? 0 :( INT) OBJ;
}< br> set
{< br> viewstate ["recordcount"] = value;
}< BR >}

[Browsable (true ),
Description ("number of records displayed per page "),
Category ("pagination "),
Defaultvalue (10)]
Public int pagesize
{
Get
{
Object OBJ = viewstate ["pagesize"];
Return (OBJ = NULL )? 10 :( INT) OBJ;
}
Set
{
Viewstate ["pagesize"] = value;
}
}

[Browsable (false ),
Description ("current display page "),
Category ("pagination "),
Defaultvalue (0)]
Public int currentpage
{
Get
{
Object OBJ = viewstate ["currentpage"];
Return (OBJ = NULL )? 0 :( INT) OBJ;
}
Set
{
Viewstate ["currentpage"] = value;
}
}


[Browsable (false ),
Description ("Total number of dataset pages "),
Category ("pagination "),
Defaultvalue (0)]
Public int pagecount
{
Get
{
Object OBJ = viewstate ["pagecount"];
Return (OBJ = NULL )? 0 :( INT) OBJ;
}
Set
{
Viewstate ["pagecount"] = value;
}
}
[Browsable (true ),
Description ("homepage button text, for example, available: First page "),
Category ("pagination")]
Public String firstpagetext
{
Get
{
Object OBJ = viewstate ["firstpagetext"];
Return (OBJ = NULL )? Null :( string) OBJ;
}
Set
{
Viewstate ["firstpagetext"] = value;
}
}
[Browsable (true ),
Description ("front page button text, for example, available: Previous Page "),
Category ("pagination")]
Public String prevpagetext
{
Get
{
Object OBJ = viewstate ["prevpagetext"];
Return (OBJ = NULL )? Null :( string) OBJ;
}
Set
{
Viewstate ["prevpagetext"] = value;
}
}

[Browsable (true ),
Description ("next page button text, for example, available: Next page "),
Category ("pagination")]
Public String nextpagetext
{
Get
{
Object OBJ = viewstate ["nextpagetext"];
Return (OBJ = NULL )? Null :( string) OBJ;
}
Set
{
Viewstate ["nextpagetext"] = value;
}
}
[Browsable (true ),
Description ("button text on the last page, for example, available: Last page "),
Category ("pagination")]
Public String lastpagetext
{
Get
{
Object OBJ = viewstate ["lastpagetext"];
Return (OBJ = NULL )? Null :( string) OBJ;
}
Set
{
Viewstate ["lastpagetext"] = value;
}
}
# Endregion
Protected override void createchildcontrols ()
{

Label lbl_pageinfo = new label (); // 0
Lbl_pageinfo.font.size = 10;
Lbl_pageinfo.text = "not bound ";
Controls. Add (lbl_pageinfo );

Linkbutton btn_first = new linkbutton (); // 1
Btn_first.text = firstpagetext. tostring ();
Btn_first.font.size = 10;
Controls. Add (btn_first );
Btn_first.visible = false;
Btn_first.click + = new system. eventhandler (btnfirst_click );

Controls. Add (New literalcontrol (""); // 2

Linkbutton btn_prev = new linkbutton (); // 3
Btn_prev.text = prevpagetext. tostring ();
Btn_prev.font.size = 10;
Controls. Add (btn_prev );
Btn_prev.visible = false;
Btn_prev.click + = new system. eventhandler (btnprev_click );

Controls. Add (New literalcontrol (""); // 4

Linkbutton btn_next = new linkbutton (); // 5
Btn_next.text = viewstate ["nextpagetext"]. tostring ();
Btn_next.font.size = 10;
Controls. Add (btn_next );
Btn_next.visible = false;
Btn_next.click + = new system. eventhandler (btnnext_click );

Controls. Add (New literalcontrol (""); // 6

Linkbutton btn_last = new linkbutton (); // 7
Btn_last.text = viewstate ["lastpagetext"]. tostring ();
Btn_last.font.size = 10;
Controls. Add (btn_last );
Btn_last.visible = false;
Btn_last.click + = new system. eventhandler (btnlast_click );

Controls. Add (New literalcontrol ("& nbsp;"); // 8

Label lbl_1 = new label (); // 9
Lbl_1.text = "go ";
Lbl_1.font.size = 10;
Lbl_1.visible = false;
Controls. Add (lbl_1 );

Dropdownlist drop_curpage = new dropdownlist (); // 10
Drop_curpage.items.add ("0 ");
Drop_curpage.font.size = 10;
Controls. Add (drop_curpage );
Drop_curpage.autopostback = true;
Drop_curpage.visible = false;
Drop_curpage.selectedindexchanged + = new system. eventhandler (ddlcurrentpage_selectedindexchanged );

label lbl_2 = new label (); // 11
lbl_2.text = "page";
lbl_2.font.size = 10;
lbl_2.visible = false;
controls. add (lbl_2);
}< br> protected override void onprerender (eventargs e)
{< br> If (! Page. ispostback)
{< br> bindpager ();
}< BR >}< br> protected virtual void pageclick (Object sender, eventargs E)
{< br> If (pagechanged! = NULL)
{< br> pagechanged (this, e);
}< BR >}

# Region page buttons and selection box events
Private void ddlcurrentpage_selectedindexchanged (Object sender, system. eventargs E)
{
Currentpage = (dropdownlist) controls [10]). selectedindex;
Bindpager ();
Sendpagechanged ();
}

Private void btnfirst_click (Object sender, system. eventargs E)
{
Currentpage = 0;
Bindpager ();
Sendpagechanged ();
}

Private void btnprev_click (Object sender, system. eventargs E)
{
Currentpage = CurrentPage-1;
Bindpager ();
Sendpagechanged ();
}

Private void btnnext_click (Object sender, system. eventargs E)
{
Currentpage = currentpage + 1;
Bindpager ();
Sendpagechanged ();
}

Private void btnlast_click (Object sender, system. eventargs E)
{
Currentpage = CurrentPage-1;
Bindpager ();
Sendpagechanged ();

}
Private void sendpagechanged ()
{
If (pagechanged! = NULL)
Pageclick (this, eventargs. Empty );

}
# Endregion
# Region binding page
Public void bindpager ()
{
Pagecount = (recordcount % pagesize> 0 )? Recordcount/pagesize + 1: recordcount/pagesize; // total number of pages
(Label) controls [0]). TEXT = "Total <font color = Red>" + recordcount. tostring () + "</font> <font color = Red>" + pagesize. tostring () + "</font> current <font color = Red>" + (currentpage + 1 ). tostring () + "</font>/<font color = Red>" + pagecount. tostring () + "</font> page & nbsp ;";
(Dropdownlist) controls [10]). Items. Clear ();
For (INT I = 0; I <pagecount; I ++)
{
(Dropdownlist) controls [10]). Items. Add (convert. tostring (I + 1 ));
}
(Dropdownlist) controls [10]). selectedindex = (dropdownlist) controls [10]). items. indexof (dropdownlist) controls [10]). items. findbyvalue (convert. tostring (currentpage + 1 )));


(Linkbutton) controls [1]). Visible = true;
(Linkbutton) controls [3]). Visible = true;
(Linkbutton) controls [5]). Visible = true;
(Linkbutton) controls [7]). Visible = true;
(Label) controls [9]). Visible = true;
(Label) controls [11]). Visible = true;
(Dropdownlist) controls [10]). Visible = true;

(Linkbutton) controls [1]). Enabled = true;
(Linkbutton) controls [3]). Enabled = true;
(Linkbutton) controls [5]). Enabled = true;
(Linkbutton) controls [7]). Enabled = true;

If (currentpage = 0)
{
(Linkbutton) controls [1]). Enabled = false;
(Linkbutton) controls [3]). Enabled = false;
(Linkbutton) controls [5]). Enabled = true;
(Linkbutton) controls [7]). Enabled = true;
}
If (currentpage = PageCount-1)
{
(Linkbutton) controls [1]). enabled = true; // 1ibtnfirst can be used for custom image buttons. attributes ["disabled"] = "disabled" cannot be clicked; 2ibtnfirst. attributes. remove ("disabled"); click
(Linkbutton) controls [3]). Enabled = true;
(Linkbutton) controls [5]). Enabled = false;
(Linkbutton) controls [7]). Enabled = false;
}
If (PageCount-1) = 0)
{
(Linkbutton) controls [1]). Enabled = false;
(Linkbutton) controls [3]). Enabled = false;
(Linkbutton) controls [5]). Enabled = false;
(Linkbutton) controls [7]). Enabled = false;
}
}
# Endregion

}
}
Call time
Private sub page_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load
'Users who place the initialization page here Code
If not ispostback then
Binddata (0)
End if
End sub
Private sub binddata (byval currentpage as integer)
Txtsql = "select * from orders"
Dbset = executepager (txtsql, currentpage, pager1, errormsg)
Datagrid2.datasource = dbset. Tables (0). defaultview
Datagrid2.databind ()
Pager1.recordcount = 47 // Of course this write is definitely wrong. Here we need to calculate the number of pages
Pager1.databind ()
End sub

Private sub change (byval sender as system. Object, byval e as system. eventargs) handles pager1.pagechanged
Binddata (pager1.currentpage)
End sub

Write a public function in the module:
Public Function executepager (byval strsql as string, byval currentpage as integer, byval pager as object, byref errmsg as string) as Dataset
Dim CNN as sqlclient. sqlconnection
Dim cmd as new sqlclient. sqlcommand
Dim adpt as sqlclient. sqldataadapter
Dim rst as new dataset
Dim splitsql () as string
Errmsg = ""
Try
CNN = new sqlclient. sqlconnection ("Data Source = (local); initial catalog = northwind; user id = sa; Pwd = 'hyaocuk! '")
Adpt = new sqlclient. sqldataadapter (strsql, CNN)
Adpt. Fill (RST, pager. pagesize * currentpage, pager. pagesize, "TB ")
'The paging efficiency is not very high, it is best to call the Stored Procedure Paging
Executepager = RST
Catch ex as exception
Errmsg = ex. Message
Finally
RST = nothing
CNN = nothing
End try
End Function

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.