C # custom Page Control

Source: Internet
Author: User

Some time ago, the intern was instructed to develop a prototype of the student management system and found that they did not have a good understanding of the custom controls. So today, I have to explain how to create the paging controls step by step, it also makes consolidation for itself.

 

1. Customize the appearance of the page control (this is simple, just drag the control)

The linkbutton control must have an onclick event.

The dropdown control must have an onselectedindexchanged event and autopostback is true.

 

Ii. Define events and Delegation
Public Delegate void onpageindexchangeddelegate (INT pageindex );

[Description ("page Jump Event")]
[Browsable (true)]
Public event onpageindexchangeddelegate onpageindexchanged = delegate {};

 

3. define relevant attributes

// Page number

Public int pagesize
{
Get
{
If (viewstate ["pagesize"] = NULL)
{
Viewstate ["pagesize"] = 5;
}

Return (INT) viewstate ["pagesize"];
}
Set
{
Viewstate ["pagesize"] = value;
}
}

// Number of records

Public int recordcount
{
Get
{
If (viewstate ["recordcount"] = NULL)
{
Viewstate ["recordcount"] = 0;
}

Return (INT) viewstate ["recordcount"];
}
Set {viewstate ["recordcount"] = value ;}
}

// Page number

Public int pagecount
{
Get {return (INT) (math. Ceiling (recordcount/(double) pagesize ));}
}

// Current page

Public int currentpageindex
{
Get
{
If (viewstate ["currentpageindex"] = NULL)
{
Viewstate ["currentpageindex"] = 1;
}
Return (INT) viewstate ["currentpageindex"];
}
Set {viewstate ["currentpageindex"] = value ;}
}

 

Iv. Related Methods

 

1. initialize the value of the control in the page_load event of the custom control.

2. Define the public method BIND ()

If recordcount is 0, all four linkbuttons are unavailable.

If pagecount is less than 1, all four linkbutton buttons cannot be used

If the current page is 1, the next and last pages are unavailable

If the current page is pagecount, the homepage and the previous page cannot be used.

3. onclick events of four linkbuttons

Homepage: the current page number is 1, and onpageindexchanged is called.

Previous Page: Current page-1, onpageindexchanged

Next page: the current page is + 1, and onpageindexchanged is called.

Last page: the current page is pagecount, onpageindexchanged

 

5. How to Use

1. Drag the custom page control to the page.

2. In pageload of the page, you must define the onpageindexchanged event of the page control. For example:

This. pager1.onpageindexchanged + = new pager. onpageindexchangeddelegate (pager1_onpageindexchanged );

3. Call the binding of the gridview in the pager1_onpageindexchanged (INT pageindex) method, and pass the pageindex and pagesize.

4. For paging SQL statements or paging stored procedures, you need to write them separately.

 

Vi. Extended Functions

By Step 5, a simple custom page control has been completed, but some page control may also have a drop-down list to select the number of entries displayed on each page, and a text box to jump to the specified page number. Some users also say that the text above the Page Control in page a is called "homepage, Previous Page, next page, and last page ", on page B, the text on the control is <, <,>,>. I can't write another one.

In fact, all these are simple. You only need to add some attributes to the paging control.

For example, to change the text content on the control, you can define a property named firstpagetext, which is used to set the button text on the home page, and so on.

Public String firstpagetext
{
Get {return lnkfirstpage. Text ;}
Set {lnkfirstpage. Text = value ;}
}

 

For example, enter the page number in the text box to jump to the corresponding page. Assign values to currentpageindex in the textchanged event of Textbox, and then call onpageindexchanged.

 

Note: I almost forgot a bit. The paging control should continue from usercontrol.

 

VII. Summary

In fact, custom controls are very powerful. We should be good at using our own brain and the development framework provided by Microsoft. Make the program magic in your own hands ~

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.