Repeater and unipager in a Custom Control (widget)

Novice User asked on February 13, 2018 00:39

I am trying to create a widget which is attempting to display pages with pagination. I am using repeater and unipager for this purpose. Problem is that repeater is displaying pages but unipager is not being displayed Any pointers would help. Thanks

Here is the code

         PageList = DocumentHelper.GetDocuments("mypage.type").Path("/%").Published().TopN(40).OrderBy("DocumentCreatedWhen desc").Published()");
        CMSRepeater1.TransformationName = "my.transformtion";
        uni1.PageControl = "CMSRepeater1";
        uni1.PageSize = 5;
        uni1.PagerMode = CMS.Controls.UniPagerMode.Querystring;
        uni1.GroupSize = 5;
        uni1.QueryStringKey = "page";
        CMSRepeater1.DataSource = ArtilceList;
        CMSRepeater1.DataBind();
        CMSRepeater1.Visible = true;

Recent Answers


Trevor Fayas answered on February 14, 2018 03:35

As much as you may have the item called "CMSRepeater1" it probably is being given a differnet identity since it's a widget and you can have multiple widgets on the same page, vs. web part must have a unique webpartID.

I don't have intellisense up with me right now, but can you do something like

uni1.PageControl = CMSRepeater1.WebpartID

or something similar?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on February 21, 2018 16:42

I've built many webparts like this which were also converted into widgets. I think you're complicating things too much with this. Let the repeater do what is is supposed to on it's own.

You're query: DocumentHelper.GetDocuments("mypage.type").Path("/%").Published().TopN(40).OrderBy("DocumentCreatedWhen desc").Published();

Can be easily accomplished by setting properties of the repeater webpart already. So what I suggest is cloning the repeater webpart and adding the unipager control to it. Here's an example of what I've done in the past. Keeping in mind when the webpart/widget is put on the page it has some design elements assigned to the layout of the webpart so you will have to clear those out. But essentially, I have a pager at the top and bottom of the page with different ID's as well as a repeater between them.

 <section id="section" runat="server">
    <cms:CMSEditModeButtonAdd ID="btnAdd" runat="server" Visible="False" />
    <asp:Panel ID="pnlTitle" runat="server" CssClass="row contain">
        <div class="col-xs-12 col-md-8 title">
            <asp:Literal ID="litTitle" runat="server" EnableViewState="false" />
        </div>
    </asp:Panel>
    <asp:Panel ID="pnlTop" runat="server" CssClass="row contain">
        <asp:Panel ID="pnlCountTop" runat="server" CssClass="col-xs-12 col-md-3">
            <asp:Literal ID="litCountTop" runat="server" EnableViewState="false" />
        </asp:Panel>
        <asp:Panel ID="pnlPagerTop" runat="server" CssClass="col-xs-12 col-md-6 col-md-push-3">
            <p class="pagination">
                <cms:UniPager ID="pagerTop" runat="server" />
            </p>
        </asp:Panel>
    </asp:Panel>
    <cms:CMSRepeater ID="repItems" runat="server" />
    <asp:Panel ID="pnlBottom" runat="server" CssClass="row contain">
        <asp:Panel ID="pnlPagerBottom" runat="server" CssClass="col-xs-12 col-md-6 col-md-push-3">
            <p class="pagination">
                <cms:UniPager ID="pagerBottom" runat="server" />
            </p>
        </asp:Panel>
        <asp:Panel ID="pnlCountBottom" runat="server" CssClass="col-xs-12 col-md-3 col-md-pull-6">
            <asp:Literal ID="litCountBottom" runat="server" EnableViewState="false" />
        </asp:Panel>
    </asp:Panel>
</section>

I've then added to the code behind to accommodate the pagers and setting their properties based on public properties I've added manually. I've not modified the SetupControl() method, that is using all the base repeater properties. You will have to add a lot of properties though to the webpart to be able to have it setup the way I do.

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    section.Attributes.Add("class", SectionClass);

    #region "Top Pager properties"

    // Set top pager properties
    pagerTop.PageControl = "repItems";
    pagerTop.PageSize = PageSize;
    pagerTop.GroupSize = GroupSize;
    pagerTop.QueryStringKey = QueryStringKey;
    pagerTop.DisplayFirstLastAutomatically = DisplayFirstLastAutomatically;
    pagerTop.DisplayPreviousNextAutomatically = DisplayPreviousNextAutomatically;
    pagerTop.HidePagerForSinglePage = HidePagerForSinglePage;
    pagerTop.ResetScrollPositionOnPostBack = ResetScrollPositionOnPostBack;

    // Set pager mode
    switch (PagingMode.ToLowerCSafe())
    {
        case "postback":
            pagerTop.PagerMode = UniPagerMode.PostBack;
            break;
        default:
            pagerTop.PagerMode = UniPagerMode.Querystring;
            break;
    }

    #endregion

    #region "Bottom pager properties"

    // Set bottom pager properties
    pagerBottom.PageControl = "repItems";
    pagerBottom.PageSize = PageSize;
    pagerBottom.GroupSize = GroupSize;
    pagerBottom.QueryStringKey = QueryStringKey;
    pagerBottom.DisplayFirstLastAutomatically = DisplayFirstLastAutomatically;
    pagerBottom.DisplayPreviousNextAutomatically = DisplayPreviousNextAutomatically;
    pagerBottom.HidePagerForSinglePage = HidePagerForSinglePage;
    pagerBottom.ResetScrollPositionOnPostBack = ResetScrollPositionOnPostBack;

    // Set pager mode
    switch (PagingMode.ToLowerCSafe())
    {
        case "postback":
            pagerBottom.PagerMode = UniPagerMode.PostBack;
            break;
        default:
            pagerBottom.PagerMode = UniPagerMode.Querystring;
            break;
    }

    #endregion

    // set custom pager properties
    // hide or show the containers if either one are checked
    pnlTop.Visible = DisplayTopCount || DisplayTopPager;
    pnlBottom.Visible = DisplayBottomCount || DisplayBottomPager;

    pnlPagerTop.Visible = DisplayTopPager;
    pnlPagerBottom.Visible = DisplayBottomPager;

    pnlCountTop.Visible = DisplayTopCount;
    pnlCountBottom.Visible = DisplayBottomCount;

    pnlTitle.Visible = !string.IsNullOrEmpty(SectionTitle);
    litTitle.Text = string.Format("<h2>{0}</h2>", SectionTitle);

    // Setting transformations for pagers
    #region "Pager templates"

    // Pages
    if (!String.IsNullOrEmpty(PagesTemplate))
    {
        pagerTop.PageNumbersTemplate = CMSDataProperties.LoadTransformation(pagerTop, PagesTemplate);
        pagerBottom.PageNumbersTemplate = CMSDataProperties.LoadTransformation(pagerBottom, PagesTemplate);
    }

    // Current page
    if (!String.IsNullOrEmpty(CurrentPageTemplate))
    {
        pagerTop.CurrentPageTemplate = CMSDataProperties.LoadTransformation(pagerTop, CurrentPageTemplate);
        pagerBottom.CurrentPageTemplate = CMSDataProperties.LoadTransformation(pagerBottom, CurrentPageTemplate);
    }

    // Separator
    if (!String.IsNullOrEmpty(SeparatorTemplate))
    {
        pagerTop.PageNumbersSeparatorTemplate = CMSDataProperties.LoadTransformation(pagerTop, SeparatorTemplate);
        pagerBottom.PageNumbersSeparatorTemplate = CMSDataProperties.LoadTransformation(pagerBottom, SeparatorTemplate);
    }

    // First page
    if (!String.IsNullOrEmpty(FirstPageTemplate))
    {
        pagerTop.FirstPageTemplate = CMSDataProperties.LoadTransformation(pagerTop, FirstPageTemplate);
        pagerBottom.FirstPageTemplate = CMSDataProperties.LoadTransformation(pagerBottom, FirstPageTemplate);
    }

    // Last page
    if (!String.IsNullOrEmpty(LastPageTemplate))
    {
        pagerTop.LastPageTemplate = CMSDataProperties.LoadTransformation(pagerTop, LastPageTemplate);
        pagerBottom.LastPageTemplate = CMSDataProperties.LoadTransformation(pagerBottom, LastPageTemplate);
    }

    // Previous page
    if (!String.IsNullOrEmpty(PreviousPageTemplate))
    {
        pagerTop.PreviousPageTemplate = CMSDataProperties.LoadTransformation(pagerTop, PreviousPageTemplate);
        pagerBottom.PreviousPageTemplate = CMSDataProperties.LoadTransformation(pagerBottom, PreviousPageTemplate);
    }

    // Next page
    if (!String.IsNullOrEmpty(NextPageTemplate))
    {
        pagerTop.NextPageTemplate = CMSDataProperties.LoadTransformation(pagerTop, NextPageTemplate);
        pagerBottom.NextPageTemplate = CMSDataProperties.LoadTransformation(pagerBottom, NextPageTemplate);
    }

    // Previous group
    if (!String.IsNullOrEmpty(PreviousGroupTemplate))
    {
        pagerTop.PreviousGroupTemplate = CMSDataProperties.LoadTransformation(pagerTop, PreviousGroupTemplate);
        pagerBottom.PreviousGroupTemplate = CMSDataProperties.LoadTransformation(pagerBottom, PreviousGroupTemplate);
    }

    // Next group
    if (!String.IsNullOrEmpty(NextGroupTemplate))
    {
        pagerTop.NextGroupTemplate = CMSDataProperties.LoadTransformation(pagerTop, NextGroupTemplate);
        pagerBottom.NextGroupTemplate = CMSDataProperties.LoadTransformation(pagerBottom, NextGroupTemplate);
    }

    // Direct page
    if (!String.IsNullOrEmpty(DirectPageTemplate))
    {
        pagerTop.DirectPageTemplate = CMSDataProperties.LoadTransformation(pagerTop, DirectPageTemplate);
        pagerBottom.DirectPageTemplate = CMSDataProperties.LoadTransformation(pagerBottom, DirectPageTemplate);
    }

    // Layout
    if (!String.IsNullOrEmpty(LayoutTemplate))
    {
        pagerTop.LayoutTemplate = CMSDataProperties.LoadTransformation(pagerTop, LayoutTemplate);
        pagerBottom.LayoutTemplate = CMSDataProperties.LoadTransformation(pagerBottom, LayoutTemplate);
    }

    #endregion

}

/// <summary>
/// OnPrerender override (Set visibility).
/// </summary>
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);

    int topStart = 1;
    int topEnd = 0;
    int bottomStart = 1;
    int bottomEnd = 1;

    topStart = (pagerTop.CurrentPage * PageSize) - (PageSize - 1);
    bottomStart = (pagerBottom.CurrentPage * PageSize) - (PageSize - 1);
    if (pagerTop.DataSourceItemsCount < pagerTop.CurrentPage * PageSize)
    {
        topEnd = pagerTop.DataSourceItemsCount;
    }
    else
    {
        topEnd = pagerTop.CurrentPage * PageSize;
    }

    if (pagerBottom.DataSourceItemsCount < pagerBottom.CurrentPage * PageSize)
    {
        bottomEnd = pagerBottom.DataSourceItemsCount;
    }
    else
    {
        bottomEnd = pagerBottom.CurrentPage * PageSize;
    }

    litCountTop.Text = string.Format(TopCountFormat, topStart.ToString(), topEnd.ToString(), pagerTop.DataSourceItemsCount.ToString());
    litCountBottom.Text = string.Format(BottomCountFormat, bottomStart.ToString(), bottomEnd.ToString(), pagerBottom.DataSourceItemsCount.ToString());

    // hide the panels if no rows and property set to true
    if (pagerTop.DataSourceItemsCount < 1 && HideTopCountIfZeroRows)
    {
        pnlCountTop.Visible = false;
    }

    if (pagerBottom.DataSourceItemsCount < 1 && HideBottomCountIfZeroRows)
    {
        pnlCountBottom.Visible = false;
    }

    repItems.CallHandled(SetVisibility);
    Page.SaveStateComplete += new EventHandler(Page_SaveStateComplete);
}  

As I mentioned, don't hard code your webpart, make it as flexible as possible so it can be reused. The way you have your webpart setup, it can only be used for only displaying 40 "Articles". Any change to that, you have to modify code vs. simply configuring the webpart.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.