Implementing the IUniPageable interface

The following is a step-by-step tutorial shows how to create a custom control that displays users and implements the IUniPageable interface. This allows the control to be paged by the UniPager.

  1. Create a new Web User Control in your web project (the example uses the name UniPageable_Repeater.ascx).

  2. Add the following markup to the user control:

    <asp:Repeater ID="Repeater1" runat="server">
    
        <ItemTemplate>
    
        <div>
            <%# Eval("UserName") %>
        </div>
    
        </ItemTemplate>
    
    </asp:Repeater>

    This adds a standard .NET Repeater control that will be used to display user names.

  3. Switch to the code behind of the control and add the following code.

    Note: The name of the class must match the name and location of your web user control.

    using CMS.Controls;
    using CMS.Membership;
    
    public partial class IUniPageableExample_UniPageable_Repeater : System.Web.UI.UserControl, IUniPageable
    {
        // Private variable containing the value of the PagerForceNumberOfResults property
        private int mPagerForceNumberOfResults = -1;
    
        // Private variable used to contain the data source of the control
        private object dataSource = null;
    
        // Private variable storing the associated UniPager object
        private UniPager mUniPagerControl = null;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            // Loads all users from the database into the data source
            dataSource = UserInfoProvider.GetUsers();
    
            // Calls the page binding event
            if (OnPageBinding != null)
            {
                OnPageBinding(this, null);
            }
    
            // Assigns the data source to the encapsulated Repeater control
            Repeater1.DataSource = dataSource;
            Repeater1.DataBind();
        }
    
        /// <summary>
        /// Occurs when the control binds page data
        /// </summary>
        public event EventHandler<EventArgs> OnPageBinding;
    
        /// <summary>
        /// Occurs when the pager changes the page and the current PagerMode is set to postback
        /// </summary>
        public event EventHandler<EventArgs> OnPageChanged;
    
        /// <summary>
        /// Exposes the data object for the pager
        /// </summary>
        public object PagerDataItem
        {
            get
            {
                return dataSource;
            }
            set
            {
                dataSource = value;
                Repeater1.DataSource = value;
            }
        }
    
        /// <summary>
        /// If set, the DataSet containing paged items is not modified by the pager,
        /// but the pager itself behaves as if the amount of paged items were identical to this value.
        /// By default this property is disabled (set to -1)
        /// </summary>
        public int PagerForceNumberOfResults
        {
            get
            {
                return mPagerForceNumberOfResults;
            }
            set
            {
                mPagerForceNumberOfResults = value;
            }
        }
    
        /// <summary>
        /// Gets or sets the pager control.
        /// </summary>
        public UniPager UniPagerControl
        {
            get
            {
                return mUniPagerControl;
            }
            set
            {
                mUniPagerControl = value;
            }
        }
    
        /// <summary>
        /// Evokes databinding for the control
        /// </summary>
        public void ReBind()
        {
            if (OnPageChanged != null)
            {
                OnPageChanged(this, null);
            }
            Repeater1.DataBind();
        }
    }
  4. Save the user control’s files.

    • The control now implements the IUniPageable interface and is pageable by the UniPager control.
  5. Create a new Web form somewhere in your web project.

  6. Register your custom control on the web form:

    <%@ Register src="~/IUniPageableExample/UniPageable_Repeater.ascx" tagname="UniPageableRepeater" tagprefix="asp1" %>
  7. Add the following code into the content area of the page (by default between the <div> tags inside the <form> element):

    <asp1:UniPageableRepeater ID="UPRepeater1" runat="server" />
    
    <cms:UniPager ID="UniPager1" runat="server" PageControl="UPRepeater1" PageSize="5">
    
        <PageNumbersTemplate>
            <a href="<%# Eval("PageURL") %>"><%# Eval("Page") %></a>
        </PageNumbersTemplate>
    
    </cms:UniPager>
  8. Save the web form.

  9. Right-click the web form in the Solution explorer and select View in Browser.

The resulting page displays a list of user names with a basic pager: