Programatically adding RibbonBarGalleryItem to Ribbon at runtime

5 posts, 0 answers
  1. Jonathan
    Jonathan avatar
    5 posts
    Member since:
    Sep 2008

    Posted 14 Jan 2014 Link to this post

    Hi,

    I'm trying to add RibbonBarGalleryItems to a RibbonBarGalleryCategory at runtime like this (I'm pulling them out of a datatable);

    RibbonBarGalleryCategory someCategory = (RibbonBarGalleryCategory)FindControlRecursive(this,"radGalleryCategory1");
     
           foreach (DataRow R in someDatatable.Rows)
           {
               RibbonBarGalleryItem myitem = new RibbonBarGalleryItem();
               myitem.ID = "someIDgeneratedbyMyApp"
               myitem.Text = R["Col1"].ToString();
               myitem.ToolTip = R["Co2"].ToString();
               myitem.ImageUrl = "~/GlobalResources/somepic.png";
               someCateogry.Items.Add(myitem);
           }

     


    This works fine - the items appear in the gallery OK on page_load and display in the browser.

    However, when I click any of the gallery items to select them, I get an index out of range exception that's not handled.

    Gallery items added at design time work fine. It's the ones that I'm adding at runtime that fail on selection.

    Is there some method I should hook into when the RadRibbonGallery is initialised so that I pass in my gallery items then, so the ribbonbar is "aware" of them?

    Thanks in advance,
    Jonathan


  2. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 17 Jan 2014 Link to this post

    Hi Jonathan,

    Are you causing a postback after you click on the gallery items? Please verify that are adding gallery items on every postback, so after the postback the selected item exists in the RibbonBar items and the control can select it properly.

    Regards,
    Hristo Valyavicharski
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jonathan
    Jonathan avatar
    5 posts
    Member since:
    Sep 2008

    Posted 17 Jan 2014 Link to this post

    HI Hristo,

    I'm adding them from the aspx.cs (code behind) inside the page_load event in code as below.

    When the browser displays the page, the items appear in the gallery OK but I get the exception when I then try to select them;

    [ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index]
       System.ThrowHelper.ThrowArgumentOutOfRangeException() +72
       System.Collections.Generic.List`1.get_Item(Int32 index) +34
       Telerik.Web.UI.RadRibbonBar.LoadGallerySelectedIndices(String[] galleryIndices) +499
       Telerik.Web.UI.RadRibbonBar.LoadPostData(String postDataKey, NameValueCollection postCollection) +536
       Telerik.Web.UI.RadWebControl.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +42
       System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +734
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)



    There is no post back event involved at the moment. Should I trigger a postback immediately after the page_load event?

    // when the page loads, update the gallery
     DataTable dtMyTable = DBEgnine.ReturnGalleryItems();
     
     // find the gallery category by ID
     RibbonBarGalleryCategory galleryYearPeriodsCategory = (RibbonBarGalleryCategory)FindControlRecursive(this,"radFavouriteYearPeriodsCategory");
     
     // cycle through every item in the table (max of about 10) and add to the gallerycategory
     foreach (DataRow R in someDatatable.Rows)
    {
        RibbonBarGalleryItem myitem = new RibbonBarGalleryItem();
        myitem.ID = "someIDgeneratedbyMyApp"
        myitem.Text = R["Col1"].ToString();
        myitem.ToolTip = R["Co2"].ToString();
        myitem.ImageUrl = "~/GlobalResources/somepic.png";
        galleryYearPeriodsCategory.Items.Add(myitem);
    }
  5. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 21 Jan 2014 Link to this post

    Hi Jonathan,

    Probably you are making a postback or ajax call on a item click and after the postback the  RibbonBarGalleryItem does not contain any items. What if you move this code in the page init event as suggested here: http://www.telerik.com/community/forums/aspnet-ajax/ribbonbar/save-ribbonbar-in-session.aspx

    Regards,
    Hristo Valyavicharski
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  6. Jonathan
    Jonathan avatar
    5 posts
    Member since:
    Sep 2008

    Posted 22 Jan 2014 Link to this post

    Hi Hristo,

    Thanks, moving from Page_Load to Page_Init fixed the problem.

    Jonathan
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017