Code-Practical tips for implementing a paging control using custom controls in asp.net

Source: Internet
Author: User
Tags prev rowcount
I. Overview

In web development, it is often necessary to display some data, and in order to facilitate typesetting and browsing, we only need to display a portion of all records. In general, we use pagination to achieve this requirement. There are a variety of ways to implement pagination, in this article, we use a paging space to record the total number of records, the current page, total pages, and page size. To have an intuitive impression, first show the effect of the control after it is run, as shown in the following illustration:


II. Implementation of the programme

In order to implement this effect graph, in asp.net, you can use the custom Controls and user Controls two ways, the user Controls is implemented in a simple way, and the way to use Controls in peace is very different, So we use custom controls implementation.
References: Professional asp.net 2.0 Server control and Component Development

implementation of pagination control

1), create a new ASP.net Server control project,
2, add a asp.net Server control item to the project and set its name to Pageon.
3), modify the class to inherit from the CompositeControl class, and modify its attribute as follows:

Copy Code code as follows:

[Defaultproperty ("PageSize")]
[ToolBoxData ("<{0}:P ageon runat=server width=100%></{0}:P ageon>")]
public class Pageon:compositecontrol

Note: Custom controls must inherit from control or their subclasses.
4), define controls that need to be grouped
Copy Code code as follows:

Label lblmessage;
LinkButton Btnfirst;
LinkButton Btnprev;
LinkButton Btnnext;
LinkButton Btnlast;
TextBox Txtgopage;
Button Btngo;

5), defines the proptery that the paging control needs to use
Paging controls mainly include attributes such as page size, current page, total number of records, and total pages, and need to be saved in ViewState, as shown in the detailed code:
Copy Code code as follows:

public int ROWCOUNT
{
Get
{
if (viewstate["m_rowcount"] = = NULL | | int. Parse (viewstate["M_rowcount"). ToString ()) < 0)
{
viewstate["M_rowcount"] = 0;
}
return int. Parse (viewstate["M_rowcount"). ToString ());
}
Set
{
if (Value < 0)
{
viewstate["M_rowcount"] = 0;
}
Else
{
viewstate["M_rowcount"] = value;
}
This. Recreatechildcontrols ();
}
}
public int Curpage
{
Get
{
if (viewstate["M_curpage"] ==null | | int. Parse (viewstate["M_curpage"). ToString ()) < 1)
{
viewstate["M_curpage"] = 1;
}
return int. Parse (viewstate["M_curpage"). ToString ());
}
Set
{
if (Value < 1)
{
viewstate["M_curpage"] = 1;
}
else if (Value > PageCount)
{
viewstate["m_curpage"] = PageCount;
}
Else
{
viewstate["m_curpage"] = value;
}
}
}
public int PageCount
{
Get
{
return rowcount/pagesize + 1;
}
}
public int PageSize
{
Get
{
if (viewstate["m_pagesize"] ==null | | int. Parse (viewstate["M_pagesize"). ToString ()) < 1)
{
viewstate["m_pagesize"] = 15;
}
return int. Parse (viewstate["M_pagesize"). ToString ());
}
Set
{
if (Value > 0)
{
viewstate["m_pagesize"] = value;
This. Recreatechildcontrols ();
}
}
}

6), creating a child space for a custom control
The subspace that generates the custom space needs to override the CreateChildControls () method in the base class control, as shown in the detailed code:
Copy Code code as follows:

protected override void CreateChildControls ()
{
Controls.clear ();
Lblmessage = new Label ();
Lblmessage.text = "Current First" + curpage + "page Total" + PageCount + "page Total" + RowCount + "record";
Lblmessage.id = "Lblmessage";
Controls.Add (lblmessage);
Btnfirst = new LinkButton ();
Btnfirst.text = "Home page";
Btnfirst.commandname = "a";
Btnfirst.id = "Btnfirst";
if (curpage <= 1)
{
btnfirst.enabled = false;
}
Controls.Add (Btnfirst);
Btnprev = new LinkButton ();
Btnprev.text = "Previous page";
Btnprev.commandname = "Prev";
Btnprev.id = "Btnprev";
if (curpage <= 1)
{
btnprev.enabled = false;
}
Controls.Add (Btnprev);
Btnnext = new LinkButton ();
Btnnext.text = "Next page";
Btnnext.commandname = "Next";
Btnnext.id = "Btnnext";
if (curpage >= PageCount)
{
btnnext.enabled = false;
}
Controls.Add (Btnnext);
Btnlast = new LinkButton ();
Btnlast.text = "Last page";
Btnlast.commandname = "Last";
Btnlast.id = "Btnlast";
if (curpage >= PageCount)
{
btnlast.enabled = false;
}
Controls.Add (Btnlast);
Txtgopage = new TextBox ();
Txtgopage.tabindex = 1;
Txtgopage.id = "Txtgopage";
TXTGOPAGE.ATTRIBUTES.ADD ("onkeyup", @ "This.value=this.value.replace" (/\d/g, ")");
TXTGOPAGE.ATTRIBUTES.ADD ("Onafterpaste", @ "This.value=this.value.replace" (/\d/g, ")");
Controls.Add (Txtgopage);
Btngo = new Button ();
Btngo.tabindex = 2;
Btngo.commandname = "Go";
Btngo.text = "Go";
Btngo.id= "Btngo";
Controls.Add (Btngo);
Debug.WriteLine ("Ffffffffffffffffffffffffffffffffffffffffffffffffff");
Base. CreateChildControls ();
}

7), define the layout of the custom control
When the 6th step is complete, all the defined controls appear sequentially on the page, but the effect is unfriendly, and if for more than one line of space, all we need to define the layout of the control, the layout of the custom control needs to override the RenderContents () method and the Tagkey property. The code in this example looks like this:
Copy Code code as follows:

protected override void RenderContents (HtmlTextWriter output)
{
Output. RenderBeginTag (htmltextwritertag.tr);
Output. AddStyleAttribute ("Text-align", "left");
Output. RenderBeginTag (HTMLTEXTWRITERTAG.TD);
Output. Write ("");
Lblmessage.rendercontrol (output);
Output. RenderEndTag ();
Output. AddStyleAttribute ("Text-align", "right");
Output. RenderBeginTag (HTMLTEXTWRITERTAG.TD);
Btnfirst.rendercontrol (output);
Output. Write ("");
Btnprev.rendercontrol (output);
Output. Write ("");
Btnnext.rendercontrol (output);
Output. Write ("");
Btnlast.rendercontrol (output);
Output. Write ("to");
Output. AddStyleAttribute (Htmltextwriterstyle.width, "30px");
Txtgopage.rendercontrol (output);
Output. Write ("page");
Btngo.rendercontrol (output);
Output. Write ("");
Output. RenderEndTag ();
Output. RenderEndTag ();
}
protected override HtmlTextWriterTag Tagkey
{
Get
{
return htmltextwritertag.table;
}
}

In the code above, we use table to lay out the layout, and we can use other layouts, such as DIV+CSS.
8), capturing and handling events for controls
After that, the code has been able to generate the effect shown at the beginning of the article, but nothing can be done, the events on the control should not be sounded, and all the event code on the control needs to be implemented to implement the events of bubbling all child controls.
First, define a delegate:
Copy Code code as follows:

public delegate void Pageoneventhandler (object sender, EventArgs args);

Second, define the events that are based on the delegate:
Copy Code code as follows:

public event Pageoneventhandler Recpagechanged;

Finally, rewrite the bubbling event and, depending on the parameter characteristics, capture the event that needs to be handled so that it invokes the desired method.
Copy Code code as follows:

protected override bool OnBubbleEvent (object source, EventArgs args)
{
BOOL handled = FALSE;
CommandEventArgs CEA = args as CommandEventArgs;
if (CEA = null)
{
return handled;
}
Switch (cea.commandname)
{
Case "a":
handled = TRUE;
Curpage = 1;
Break
Case "Prev":
handled = TRUE;
if (Curpage > 1)
{
curpage--;
}
Else
{
Curpage = 1;
}
Break
Case "Next":
handled = TRUE;
if (Curpage < PageCount)
{
Curpage + +;
}
Else
{
Curpage = PageCount;
}
Break
Case "Last":
handled = TRUE;
Curpage = PageCount;
Break
Case "Go":
String Strgo = TxtGoPage.Text.Trim ();
int iGo;
if (!string. IsNullOrEmpty (strgo) && Int. TryParse (Strgo, out IGo))
{
handled = TRUE;
Curpage = IGo;
}
Break
}
if (handled)
{
if (this. recpagechanged!= null)
{
Recpagechanged (this, args);
This. Recreatechildcontrols ();
}
return handled;
}
Else
{
Return base. OnBubbleEvent (source, args);
}
}

This completes the development of the paging control.

Note: You can customize the style of the control, or use properties to expose the properties of child controls to control styles, and so on.
Four, use the paging control
After you have completed the development of your custom control, you can display the custom control in toolbox by choose Items in Toolbox or by referencing the project or DLL directly in the project where you want to use the custom control. You can then drag and drop the paging control where you want it, as simple as using a button control.
Then, in the OnLoad event for the background code of the page, register the method that needs to be called into the Recpagechanged event of the control, as follows:
Copy Code code as follows:

Pageon1.recpagechanged + = new Customcontrol.pageoneventhandler (pageon1_recpagechanged);

Finally, you just need to write your own code in the method pageon1_recpagechanged.
Copy Code code as follows:

void Pageon1_recpagechanged (object sender, EventArgs args)
{
To do something
}

The detailed code for the control is as follows:
Copy Code code as follows:

Namespace CustomControl
{
public delegate void Pageoneventhandler (object sender, EventArgs args);
[Defaultproperty ("PageSize")]
[ToolBoxData ("<{0}:P ageon runat=server width=100%></{0}:P ageon>")]
public class Pageon:compositecontrol
{
#region
Label lblmessage;
LinkButton Btnfirst;
LinkButton Btnprev;
LinkButton Btnnext;
LinkButton Btnlast;
TextBox Txtgopage;
Button Btngo;
#endregion
protected override void CreateChildControls ()
{
Controls.clear ();
Lblmessage = new Label ();
Lblmessage.text = "Current First" + curpage + "page Total" + PageCount + "page Total" + RowCount + "record";
Lblmessage.id = "Lblmessage";
Controls.Add (lblmessage);
Btnfirst = new LinkButton ();
Btnfirst.text = "Home page";
Btnfirst.commandname = "a";
Btnfirst.id = "Btnfirst";
if (curpage <= 1)
{
btnfirst.enabled = false;
}
Controls.Add (Btnfirst);
Btnprev = new LinkButton ();
Btnprev.text = "Previous page";
Btnprev.commandname = "Prev";
Btnprev.id = "Btnprev";
if (curpage <= 1)
{
btnprev.enabled = false;
}
Controls.Add (Btnprev);
Btnnext = new LinkButton ();
Btnnext.text = "Next page";
Btnnext.commandname = "Next";
Btnnext.id = "Btnnext";
if (curpage >= PageCount)
{
btnnext.enabled = false;
}
Controls.Add (Btnnext);
Btnlast = new LinkButton ();
Btnlast.text = "Last page";
Btnlast.commandname = "Last";
Btnlast.id = "Btnlast";
if (curpage >= PageCount)
{
btnlast.enabled = false;
}
Controls.Add (Btnlast);
Txtgopage = new TextBox ();
Txtgopage.tabindex = 1;
Txtgopage.id = "Txtgopage";
TXTGOPAGE.ATTRIBUTES.ADD ("onkeyup", @ "This.value=this.value.replace" (/\d/g, ")");
TXTGOPAGE.ATTRIBUTES.ADD ("Onafterpaste", @ "This.value=this.value.replace" (/\d/g, ")");
Controls.Add (Txtgopage);
Btngo = new Button ();
Btngo.tabindex = 2;
Btngo.commandname = "Go";
Btngo.text = "Go";
Btngo.id= "Btngo";
Controls.Add (Btngo);
Debug.WriteLine ("Ffffffffffffffffffffffffffffffffffffffffffffffffff");
Base. CreateChildControls ();
}
public int ROWCOUNT
{
Get
{
if (viewstate["m_rowcount"] = = NULL | | int. Parse (viewstate["M_rowcount"). ToString ()) < 0)
{
viewstate["M_rowcount"] = 0;
}
return int. Parse (viewstate["M_rowcount"). ToString ());
}
Set
{
if (Value < 0)
{
viewstate["M_rowcount"] = 0;
}
Else
{
viewstate["M_rowcount"] = value;
}
This. Recreatechildcontrols ();
}
}
public int Curpage
{
Get
{
if (viewstate["M_curpage"] ==null | | int. Parse (viewstate["M_curpage"). ToString ()) < 1)
{
viewstate["M_curpage"] = 1;
}
return int. Parse (viewstate["M_curpage"). ToString ());
}
Set
{
if (Value < 1)
{
viewstate["M_curpage"] = 1;
}
else if (Value > PageCount)
{
viewstate["m_curpage"] = PageCount;
}
Else
{
viewstate["m_curpage"] = value;
}
}
}
public int PageCount
{
Get
{
return rowcount/pagesize + 1;
}
}
public int PageSize
{
Get
{
if (viewstate["m_pagesize"] ==null | | int. Parse (viewstate["M_pagesize"). ToString ()) < 1)
{
viewstate["m_pagesize"] = 15;
}
return int. Parse (viewstate["M_pagesize"). ToString ());
}
Set
{
if (Value > 0)
{
viewstate["m_pagesize"] = value;
This. Recreatechildcontrols ();
}
}
}
protected override void RenderContents (HtmlTextWriter output)
{
Output. RenderBeginTag (htmltextwritertag.tr);
Output. AddStyleAttribute ("Text-align", "left");
Output. RenderBeginTag (HTMLTEXTWRITERTAG.TD);
Output. Write ("");
Lblmessage.rendercontrol (output);
Output. RenderEndTag ();
Output. AddStyleAttribute ("Text-align", "right");
Output. RenderBeginTag (HTMLTEXTWRITERTAG.TD);
Btnfirst.rendercontrol (output);
Output. Write ("");
Btnprev.rendercontrol (output);
Output. Write ("");
Btnnext.rendercontrol (output);
Output. Write ("");
Btnlast.rendercontrol (output);
Output. Write ("to");
Output. AddStyleAttribute (Htmltextwriterstyle.width, "30px");
Txtgopage.rendercontrol (output);
Output. Write ("page");
Btngo.rendercontrol (output);
Output. Write ("");
Output. RenderEndTag ();
Output. RenderEndTag ();
}
protected override HtmlTextWriterTag Tagkey
{
Get
{
return htmltextwritertag.table;
}
}
public event Pageoneventhandler Recpagechanged;
protected override bool OnBubbleEvent (object source, EventArgs args)
{
BOOL handled = FALSE;
CommandEventArgs CEA = args as CommandEventArgs;
if (CEA = null)
{
return handled;
}
Switch (cea.commandname)
{
Case "a":
handled = TRUE;
Curpage = 1;
Break
Case "Prev":
handled = TRUE;
if (Curpage > 1)
{
curpage--;
}
Else
{
Curpage = 1;
}
Break
Case "Next":
handled = TRUE;
if (Curpage < PageCount)
{
Curpage + +;
}
Else
{
Curpage = PageCount;
}
Break
Case "Last":
handled = TRUE;
Curpage = PageCount;
Break
Case "Go":
String Strgo = TxtGoPage.Text.Trim ();
int iGo;
if (!string. IsNullOrEmpty (strgo) && Int. TryParse (Strgo, out IGo))
{
handled = TRUE;
Curpage = IGo;
}
Break
}
if (handled)
{
if (this. recpagechanged!= null)
{
Recpagechanged (this, args);
This. Recreatechildcontrols ();
}
return handled;
}
Else
{
Return base. OnBubbleEvent (source, args);
}
}
}
}

OK, Finish
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.