Unable to cast object of type System.Web.UI.LiteralControl to type Telerik.Web.UI.RadPanelBar

5 posts, 0 answers
  1. Kevin
    Kevin avatar
    15 posts
    Member since:
    Mar 2013

    Posted 09 Apr 2013 Link to this post

    Hi,

    In the code below I keep getting an error on this line when the code executes: RadPanelBar rpb = (RadPanelBar)pv.Controls[0];
    The error is an unable to cast object error.

    Any idea how to resolve this? 

    protected void RadButtonExpandAll_Click(object sender, EventArgs e)
            {
                int selectedIndex = RadTabStrip1.SelectedIndex;
                if (selectedIndex >= 0)
                {
                    RadPageView pv = RadMultiPage1.PageViews[selectedIndex];
                    RadPanelBar rpb = (RadPanelBar)pv.Controls[0]; // This is what errors //


                    foreach (RadPanelItem r in rpb.Items)
                    {
                        r.Expanded = true;

                    }

                }
            }

    Thanks,

    Kevin


  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 10 Apr 2013 Link to this post

    Hi,

    I am not quiet sure about your requirement. I suppose you want to access the panelbar in button click. Here is the sample code.
    c#:
    RadPageView pv = RadMultiPage1.PageViews[selectedIndex];
    RadPanelBar rpb = (RadPanelBar)pv.FindControl("RadPanelBar1");

    Thanks,
    Shinu
  3. Kevin
    Kevin avatar
    15 posts
    Member since:
    Mar 2013

    Posted 10 Apr 2013 Link to this post

    Hi Shinu,

    Interesting thing is from what I have been told is that code used to work.
    We dynamically generate the page views and panel bars in the code behind while the page loads.
    So I can't find a specific Panel Bar with the Find Control because the name of of the bar they clicked on was created dynamically.

    That is why they put this code in the expand method:

    protected void RadButtonExpandAll_Click(object sender, EventArgs e)
            {
                int selectedIndex = RadTabStrip1.SelectedIndex;
                if (selectedIndex >= 0)
                {
                    RadPageView pv = RadMultiPage1.PageViews[selectedIndex];
                    RadPanelBar rpb = (RadPanelBar)pv.Controls[0]; // This is what errors //


                    foreach (RadPanelItem r in rpb.Items)
                    {
                        r.Expanded = true;

                    }

                }
            }

    We look for the pageview based on which tab they selected and then we locate all the controls on that panel and expand them all. Did something change with the newer versions of the Telerik libraries that would prevent this code from working?
    I am sure what version it used to work in but I have been told it did.

    Thanks,

    Kevin
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 11 Apr 2013 Link to this post

    Hi,

    Unfortunately I cannot replicate the issue at my end. Here is the full code that I tried.
    aspx:
            RadTab tab = new RadTab();
            tab.Text = "tabName";
            RadTabStrip1.Tabs.Add(tab);
            RadPageView pageView = new RadPageView();
            pageView.ID = "tabName";
            RadMultiPage1.PageViews.Add(pageView);
            RadPanelBar bar = new RadPanelBar();
            bar.ID = "panel";
            RadPanelItem item1 = new RadPanelItem();
            item1.Text = "test1";
            RadPanelItem item2 = new RadPanelItem();
            item2.Text = "test2";
            bar.Items.Add(item1);
            bar.Items.Add(item2);
            pageView.Controls.Add(bar);
    protected void Button2_Click(object sender, EventArgs e)
    {
          RadPanelBar bar = (RadPanelBar)pageView.FindControl("panel");
     }

    Thanks,
    Shinu
  5. Kevin
    Kevin avatar
    15 posts
    Member since:
    Mar 2013

    Posted 11 Apr 2013 Link to this post

    Thanks Shinu,

    Boyan game me this code which seems to work.

    protected void RadButton1_Click(object sender, EventArgs e)
        {
            RadPageView pv = RadMultiPage1.PageViews[selectedIndex];
            ControlCollection pageViewControls = pv.Controls;
            for (int i = 0; i < pageViewControls.Count; i++)
            {
                if (pageViewControls[i].GetType() == typeof(RadPanelBar))
                {
                    RadPanelBar rpb = (RadPanelBar)pageViewControls[i];
     
                    foreach (RadPanelItem r in rpb.Items)
                    {
                        r.Expanded = true;
                    }
                }      
            }
        }

    Thanks for your help.

    Kevin
Back to Top