A simple ASP.MVC paging control

Source: Internet
Author: User

Paging functionality to achieve a reference to some of the content on the Web, plus some of their own changes, the use of the following methods:

(Since the paging code is basically the same as anyone else's, it's omitted here.) )

Encapsulation extension method

 public static string Pager (this htmlhelper HTML, ipagedata model, BOOL Alwaysshow,bool shownumber) 


{


string text = "<!--does not have any data, so the paging control is not displayed-->";


BOOL show = model. TotalCount > 0 && alwaysshow | | Model. TotalCount > Model. PageSize;


if (show)


{


StringBuilder sb = new StringBuilder ();


var requrl = html. ViewContext.RequestContext.HttpContext.Request.Url.ToString ();


string link = "";


if (requrl.indexof ("page") >= 0)


{


regex re = new regex (@ "page= (\d+)", regexoptions.ignorecase);


link = Re. Replace (Requrl, "page=%7bpage%7d");


}


Else if (Requrl.indexof ("?") < 0)


{


link = requrl + "? page=%7bpage%7d";


}


Else


{


link = requrl + "&page=%7bpage%7d";


}


#region generate four buttons


Tagbuilder root = new Tagbuilder ("div");


Root. addCssClass ("Zuiepager");


Tagbuilder fbtn = new Tagbuilder ("a");


Fbtn.addcssclass ("btn");


Fbtn.mergeattribute ("style", "font-family:webdings;");


fbtn.innerhtml = "9";


Tagbuilder pbtn = new Tagbuilder ("a");


Pbtn.addcssclass ("btn");


Pbtn.mergeattribute ("style", "font-family:webdings;");


pbtn.innerhtml = "3";


Tagbuilder nbtn = new Tagbuilder ("a");


Nbtn.addcssclass ("btn");


Nbtn.mergeattribute ("style", "font-family:webdings;");


nbtn.innerhtml = "4";


Tagbuilder lbtn = new Tagbuilder ("a");


Lbtn.addcssclass ("btn");


Lbtn.mergeattribute ("style", "font-family:webdings;");


lbtn.innerhtml = ":";


#endregion


int p = model. PageIndex;


#region judge the end page


if (model. Haspreviouspage)


{


string pUrl = link. Replace ("%7bpage%7d", (p-1). ToString ());


string fUrl = link. Replace ("%7bpage%7d", (1). ToString ());


fbtn.mergeattribute ("href", fUrl);


pbtn.mergeattribute ("href", pUrl);


}


Else


{


fbtn.mergeattribute ("Disabled", "disabled");


pbtn.mergeattribute ("Disabled", "disabled");


}


if (model. Hasnextpage)


{


string nurl = link. Replace ("%7bpage%7d", (P + 1). ToString ());


string lurl = link. Replace ("%7bpage%7d", model. Totalpages.tostring ());


Nbtn.mergeattribute ("href", nurl);


lbtn.mergeattribute ("href", lurl);


}


Else


{


nbtn.mergeattribute ("Disabled", "disabled");


lbtn.mergeattribute ("Disabled", "disabled");


}


#endregion


sb. Append (Fbtn.tostring ());


sb. Append (Pbtn.tostring ());


#region Generate intermediate digital


if (shownumber)


{


int begin = p-5;


int end = p + 5;


if (begin < 1)


{


begin = 1;


end = 11;


}


if (End > Model. TotalPages)


{


end = model. TotalPages;


begin = END > 11? End-10:1;


}


for (int i = begin; I <= end; i++)


{


Tagbuilder num = new Tagbuilder ("span");


if (p = = i)


{


Num. addCssClass ("current");


Num. InnerHtml = i.ToString ();


}


Else


{


string url = link. Replace ("%7bpage%7d", i.ToString ());


Num. addCssClass ("num");


Tagbuilder a = new Tagbuilder ("a");


a.mergeattribute ("href", url);


a.innerhtml = i.ToString ();


Num. InnerHtml = a.tostring () + "";


}


sb. Append (Num. ToString ());


}


}


#endregion


sb. Append (Nbtn.tostring ());


sb. Append (Lbtn.tostring ());


#region Generate page Information


Tagbuilder index = new Tagbuilder ("span");


index. addCssClass ("num");


index. InnerHtml = P.tostring ();


Tagbuilder PageCount = new Tagbuilder ("span");


PageCount. addCssClass ("num");


PageCount. InnerHtml = model. Totalpages.tostring ();


Tagbuilder pagesize = new Tagbuilder ("span");


pagesize. addCssClass ("num");


pagesize. InnerHtml = model. Pagesize.tostring ();


Tagbuilder count = new Tagbuilder ("span");


count. addCssClass ("num");


count. InnerHtml = model. Totalcount.tostring ();


sb. Append (String. Format ("{0} page/total {1} pages per page {2} Records total {3}", index. ToString (), PageCount. ToString (), pagesize. ToString (), Count. ToString ()));


Root. InnerHtml = sb. ToString ();


Text = root. ToString ();


#endregion


}


return text;


}

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.