Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
All-Star
154957 Points
19873 Posts
Moderator
MVP
Feb 07, 2012 05:48 PM|LINK
The following code shows a helper that will split an article into pages:
@using System.Text.RegularExpressions; @helper PageArticle(string input, string delimiter){ var request = HttpContext.Current.Request; var thisPage = request.ServerVariables["SCRIPT_NAME"]; var pageNo = Convert.ToInt32(request["Page"]) == 0 ? 1 : Convert.ToInt32(request["Page"]); var pages = Regex.Split(input, delimiter); @pages[pageNo - 1] <br /> if(pages.Length > 1){ @:Go To Page: for(var i = 1; i <= pages.Length; i++){ <a href="@thisPage?Page=@i">@i</a> } } }
Put that in a file called Helpers in App_Code folder. Then this example shows how to use it with a sample test string:
@{ var input = "Page 1 ¬¬ Page 2 ¬¬ Page 3 ¬¬ Page 4"; var delimiter = "¬¬"; } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> @Helpers.PageArticle(input, delimiter) </body> </html>
Mikesdotnett...
All-Star
154957 Points
19873 Posts
Moderator
MVP
Re: Will this work in WebMatrix?
Feb 07, 2012 05:48 PM|LINK
The following code shows a helper that will split an article into pages:
@using System.Text.RegularExpressions; @helper PageArticle(string input, string delimiter){ var request = HttpContext.Current.Request; var thisPage = request.ServerVariables["SCRIPT_NAME"]; var pageNo = Convert.ToInt32(request["Page"]) == 0 ? 1 : Convert.ToInt32(request["Page"]); var pages = Regex.Split(input, delimiter); @pages[pageNo - 1] <br /> if(pages.Length > 1){ @:Go To Page: for(var i = 1; i <= pages.Length; i++){ <a href="@thisPage?Page=@i">@i</a> } } }Put that in a file called Helpers in App_Code folder. Then this example shows how to use it with a sample test string:
@{ var input = "Page 1 ¬¬ Page 2 ¬¬ Page 3 ¬¬ Page 4"; var delimiter = "¬¬"; } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> @Helpers.PageArticle(input, delimiter) </body> </html>Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter