LoadXml Error

3 posts, 1 answers
  1. Jeff Paetkau
    Jeff Paetkau avatar
    35 posts
    Member since:
    Jun 2012

    Posted 07 Oct 2011 Link to this post

    Consider the following code:

    <telerik:RadRibbonBar ID="Menu" runat="server" OnButtonToggle="ButtonToggle" />

    private void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            String Menu_XML = new XElement("RibbonBar"
                    , new XElement("Tab", new XAttribute("Text", "View")
                        , new XElement("Group", new XAttribute("Text", "Columns")
                            , new XElement("ToggleButton", new XAttribute("Text", "test"))))).ToString();
            Menu.LoadXml(Menu_XML);
        }
    }
     
    protected void ButtonToggle(object sender, System.EventArgs e)
    {
    }

    Produces the following error when you toggle the button

    Index was out of range. Must be non-negative and less than the size of the collection.

    Parameter name: indexProduces the following error when you toggle the button


    However, the following code works as expected:

    <telerik:RadRibbonBar ID="Menu" runat="server" OnButtonToggle="ButtonToggle">
        <telerik:RibbonBarTab Text="View">
            <telerik:RibbonBarGroup Text="Columns">
                <Items>
                    <telerik:RibbonBarToggleButton Text="Test" />
                </Items>
            </telerik:RibbonBarGroup>
        </telerik:RibbonBarTab>
    </telerik:RadRibbonBar>

    What am I doing wrong?
  2. Answer
    Kate
    Admin
    Kate avatar
    1898 posts

    Posted 12 Oct 2011 Link to this post

    Hi Jeff,

    The error that you encounter occurs because every time the page posts back the tab, the group and the toggle button get lost. Therefore the approach that you need to apply is to create them at an earlier stage, for example OnInit. Here you can read more about how to dynamically create controls in ASP.NET.  You can also refer to the modified code below:

    markup:
    <telerik:RadRibbonBar ID="Menu" runat="server" OnButtonToggle="ButtonToggle">
            </telerik:RadRibbonBar>

    code behind:
    protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            addTab();
        }
     
        protected void Page_Load(object sender, EventArgs e)
        {
        }
     
        private void addTab()
        {
            String Menu_XML = new XElement("RibbonBar", new XElement("Tab",
                         new XAttribute("Text", "ViewCodeBehind"),
                         new XElement("Group", new XAttribute("Text", "Columns"),
                             new XElement("ToggleButton", new XAttribute("Text", "test"))))).ToString();
            Menu.LoadXml(Menu_XML);
        }
     
        protected void ButtonToggle(object sender, System.EventArgs e)
        {
        }


    All the best,
    Kate
    the Telerik team
    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 their blog feed now
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jeff Paetkau
    Jeff Paetkau avatar
    35 posts
    Member since:
    Jun 2012

    Posted 12 Oct 2011 Link to this post

    Thanks.
Back to Top