This is a migrated thread and some comments may be shown as answers.

Programatically adding RibbonBarGalleryItem to Ribbon at runtime

4 Answers 63 Views
RibbonBar
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 14 Jan 2014, 06:12 PM

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


4 Answers, 1 is accepted

Sort by
0
Hristo Valyavicharski
Telerik team
answered on 17 Jan 2014, 03:10 PM
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.
0
Jonathan
Top achievements
Rank 1
answered on 17 Jan 2014, 04:45 PM
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);
}
0
Hristo Valyavicharski
Telerik team
answered on 21 Jan 2014, 02:37 PM
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.
0
Jonathan
Top achievements
Rank 1
answered on 22 Jan 2014, 03:08 PM
Hi Hristo,

Thanks, moving from Page_Load to Page_Init fixed the problem.

Jonathan
Tags
RibbonBar
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Hristo Valyavicharski
Telerik team
Jonathan
Top achievements
Rank 1
Share this question
or