ASP. net mvc paging implementation-added multiple tabs for the same view, asp. netmvc

Source: Internet
Author: User

ASP. net mvc paging implementation-added multiple tabs for the same view, asp. netmvc

I have implemented ASP before. net mvc paging (view this blog post), but it has limitations. You must ensure that only one page is allowed in the same view. If you need to set multiple pages in the same view, I have optimized the original code, added more flexible attribute configuration and generation rules, and solved the above problem. The Code is as follows:

1. PageInfo class


Using System; using System. collections. generic; using System. linq; using System. web; namespace ROIS. models {/// <summary> /// page information /// </summary> public class PageInfo {private int _ RecordCount = 0; private int _ PageSize = 10; private int _ CurrentPageNo = 1; private string _ PageNoCtrlName = "_ pageno"; private bool _ CreateScript = true; /// <summary> /// obtain or set the total number of records /// </summary> public int RecordCount {Get {return _ RecordCount;} set {if (value> 0) {_ RecordCount = value ;}}} /// <summary> /// get or set the number of records per page /// </summary> public int PageSize {get {return _ PageSize ;} set {if (value> 0) {_ PageSize = value ;}}/// <summary> // obtain or set the current index page number (calculated from 1) /// </summary> public int CurrentPageNo {get {return _ CurrentPageNo;} set {if (value> 0) {if (value> this. pageCount) {_ CurrentPageN O = this. pageCount;} else {_ CurrentPageNo = value ;}}}} /// <summary> /// obtain the total number of pages /// </summary> public int PageCount {get {if (this. recordCount <= 0) {return 1;} return this. recordCount/this. pageSize + (this. recordCount % this. pageSize> 0? 1: 0 );}} /// <summary> /// obtain or set the control name for storing the page number. // </summary> public string PageNoCtrlName {get {return _ PageNoCtrlName;} set {if (! String. isNullOrEmpty (value) {_ PageNoCtrlName = value ;}}} /// <summary> /// whether to create a script function // </summary> public bool CreateScript {get {return _ CreateScript ;} set {_ CreateScript = value ;}} public PageInfo () {}public PageInfo (int recordCount, int currentPageNo, int pageSize = 10, string pageNoCtrlName = null, bool createScript = true) {this. recordCount = recordCount; this. pageSize = pageSize; this. currentPageNo = currentPageNo; this. pageNoCtrlName = pageNoCtrlName; this. createScript = createScript;} // <summary> // whether it is the Home Page // </summary> /// <returns> </returns> public bool IsFirstPage () {return (this. currentPageNo <= 1 );} /// <summary> /// whether it is the last page // </summary> /// <returns> </returns> public bool IsLastPage () {return (this. currentPageNo> = this. pageCount );}}}

Ii. _ Pager local view (it is recommended to put it under the Shared directory)


@ Using ROIS. Models; @ model PageInfo @ if (Model! = Null & Model. recordCount> 0) {<div class = "pager"> @ (Model. currentPageNo) Page & nbsp;/& nbsp; @ (@ Model. pageCount page, @ if (Model. isFirstPage () {<span >|& lt; Header & nbsp; page </span> <span> & lt; previous Page </span>} else {<a href = "javascript: turnPage (1," @ Model. pageNoCtrlName ");" >|& lt; Header & nbsp; page </a> <a href = "javascript: turnPage (@ (Model. currentPageNo-1), "@ Model. pageNoCtrlName ");"> & lt; previous page </a >}@ if (Model. isLastPage ()){ <Span> next page & gt; </span> <span> last & nbsp; Page & gt; | </span>} else {<a href = "javascript: turnPage (@ (Model. currentPageNo + 1), "@ Model. pageNoCtrlName ");"> next page & gt; </a> <a href = "javascript: turnPage (@ Model. pageCount, "@ Model. pageNoCtrlName ");"> last & nbsp; Page & gt; | </a >}go to: <select id = "pages" onchange = "javascript: turnPage (this. value, "@ Model. pageNoCtrlName ");"> @ for (int I = 1; I <= Model. pageCount; I ++) {if (Model. cur Repeated pageno = I) {<option value = "@ I" selected = "selected"> NO. @ (I) page </option>} else {<option value = "@ I"> NO. @ (I) page </option >}</select> <input type = "hidden" id = "@ Model. pageNoCtrlName "name =" @ Model. pageNoCtrlName "/> </div> if (Model. createScript) {<script type = "text/javascript"> <! -- Function turnPage (pageNo, ctrlId) {var oPageNo = document. getElementById (ctrlId); oPageNo. value = pageNo; oPageNo. form. submit ();} function getForm (obj) {if (obj. parentNode. nodeName. toLowerCase () = "form") {return obj. parentNode;} else {getForm (obj. parentNode) ;}}// --> </script> }}

Iii. Usage: add the following to the Action of the background Controller:

String pageNo = Request. form ["_ pageno"]; int iPageNo = 1; int. tryParse (pageNo, out iPageNo); PageInfo pageInfo = new PageInfo (detailList. count (), iPageNo, 30, "_ pageno", false); ViewBag. pageInfo = pageInfo;
PageNo = Request. form ["_ pageno2"]; iPageNo = 1; int. tryParse (pageNo, out iPageNo); PageInfo pageInfo2 = new PageInfo (detailList2.Count (), iPageNo, 30, "_ pageno2", true );
ViewBag. PageInfo2 = pageInfo2;

The code of the front-end VIEW page is as follows: (Note: ROIS is the name of my project, which should be changed according to the actual situation)

@ Html. Partial ("_ Pager", ViewBag. PageInfo as ROIS. Models. PageInfo) @ Html. Partial ("_ Pager", ViewBag. PageInfo2 as ROIS. Models. PageInfo)

Article published on my personal website: http://www.zuowenjun.cn/post/2014/10/23/65.html


Aspnet MVC3 achieves paging Effect

Paging, in fact, MVC is tied to link to call the background action. Therefore, you only need to add a page with the parameter on the page button, and query the page with the parameter when you go to the action, put it back into a view.
I cannot describe it clearly. You can check the buttons on Baidu pages. In fact, there is a parameter in the link that indicates the page number, and you will know how to paging.

How to add paging display to the aspnet page?

The core principle of the DataList paging method is to use the PagedDataSource object. The PagedDataSource class encapsulates the properties of the DataGrid control. These attributes enable the DataGrid to execute pagination. below is the public attribute of PagedDataSource:

AllowCustomPaging gets or sets the value indicating whether to enable custom paging.
AllowPaging gets or sets the value indicating whether to enable pagination.
Count to obtain the number of items to be used from the data source.
CurrentPageIndex gets or sets the index of the current page.
DataSource obtains or sets the data source.
Cececount gets the number of items in the data source.
FirstIndexInPage gets the first index on the page.
IsCustomPagingEnabled gets a value indicating whether to enable custom paging.
IsFirstPage gets a value indicating whether the current page is a homepage.
IsLastPage gets a value indicating whether the current page is the last page.
IsPagingEnabled gets a value indicating whether to enable paging.
IsReadOnly gets a value indicating whether the data source is read-only.
IsSynchronized gets a value indicating whether to synchronize access to the data source (thread safety ).
PageCount obtains the total number of pages required for all items in the data source.
PageSize gets or sets the number of items to be displayed on a single page.
VirtualCount gets or sets the actual number of items in the data source when custom pages are used.
So how to use the PagedDataSource object for DataList paging? (There are a lot of articles on the Internet that talk about DataList pages, some of which are confusing and some of which cannot work yet. I have been cheated once. This article is obtained through my personal practical experience, it can be executed smoothly. Hope to help you .)

Start pulling now!

Step 1: extract the data to the able, obtain the dataview, and pay it to the PagedDataSource object.

DataView objView = objTable. DefaultView;
PagedDataSource objPds = new PagedDataSource ();
ObjPds. DataSource = objView;
Step 2: configure the object objPds of PagedDataSource

ObjPds. AllowPaging = true;
ObjPds. PageSize = 4;
ObjPds. CurrentPageIndex = int. Parse (ViewState ["pageindex"]. ToString ());
The above two pieces of code are written in the BindData () function for each call when you click the paging button. The BindData () function is as follows:

Private void BindData ()
{
String SQL = "SELECT * From team ";

DataTable objTable = data. GetDataTable (SQL );
If (objTable! = Null & objTable. Rows. Count> 0)
{
DataView objView = objTable. DefaultView;
PagedDataSource objPds = new PagedDataSource ();
ObjPds. DataSource = objView;

Objpps. AllowPaging = true; ...... the remaining full text >>>

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.