This is part of a larger project and perhaps in the future I will present the finished project, but for now this user control can help us organize our data with a data list.
- Create a new web site project in Visual Studio
- Add a new Web User control named Pager.
- In Source View (Asp.Net) write the following code
- <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Pager.ascx.cs" Inherits="UserControls_Pager" %><p>Page<asp:Label ID="currentPageLabel" runat="server" />of<asp:Label ID="howManyPagesLabel" runat="server" />
<asp:HyperLink ID="previousLink" runat="server">Previous</asp:HyperLink>
<asp:Repeater ID="pagesRepeater" runat="server"><ItemTemplate><asp:HyperLink ID="hyperlink" runat="server" Text='<%#Eval("Page") %>' NavigateUrl='<%#Eval("Url") %>' /></ItemTemplate></asp:Repeater><asp:HyperLink ID="nextLink" runat="server">Next</asp:HyperLink></p> - Switch to design view and you should see the following:
- Code to the code behind file and write the following code:
- Note: I have created a simple struct called PageUrl to create an association between the page number and the Url of the link button. In other words, each time we need to go to the next page, we need to know the page number and url for that page.
- using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;
//Struct that represents a page number and url associationpublic struct PageUrl{private string page;private string url;
public string Page{get { return page; }}
public string Url{get { return url; }}
//constructorpublic PageUrl(string page, string url){this.page = page;this.url = url;}}
public partial class UserControls_Pager : System.Web.UI.UserControl{
//show the pagerpublic void Show(int currentPage, int howManyPages, string firstPageUrl, string pageUrlFormat, bool showPages){//Display pagin controlsif (howManyPages > 1){//Make pager visiblethis.Visible = true;
//Display the current pagecurrentPageLabel.Text = currentPage.ToString();howManyPagesLabel.Text = howManyPages.ToString();
//Create the "previous" linkif (currentPage == 1)previousLink.Enabled = false;elsepreviousLink.NavigateUrl = (currentPage == 2) ? firstPageUrl : String.Format(pageUrlFormat, currentPage - 1);
//Create the "Next" linkif (currentPage == howManyPages)nextLink.Enabled = false;elsenextLink.NavigateUrl = String.Format(pageUrlFormat, currentPage + 1);
//Create the page linksif (showPages){//Set the list of pages and their Urls as an arrayPageUrl[] pages = new PageUrl[howManyPages];
//Create page Url and page elements in the arraypages[0] = new PageUrl("1", firstPageUrl);for (int i = 2; i <= howManyPages; i++){pages[i - 1] = new PageUrl(i.ToString(), String.Format(pageUrlFormat, i));}
//If this is the current page, don't create a linkpages[currentPage - 1] = new PageUrl((currentPage).ToString(), "");
//set the repeaterpagesRepeater.DataSource = pages;pagesRepeater.DataBind();}}}
protected void Page_Load(object sender, EventArgs e){}}
No comments:
Post a Comment
Thank you for your thoughts. Your comment will appear in my blog shortly after review.
Have a great day!