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

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

4 Answers 355 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 09 Apr 2013, 02:25 PM
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


4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 10 Apr 2013, 05:45 AM
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
0
Kevin
Top achievements
Rank 1
answered on 10 Apr 2013, 12:49 PM
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
0
Shinu
Top achievements
Rank 2
answered on 11 Apr 2013, 06:54 AM
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
0
Kevin
Top achievements
Rank 1
answered on 11 Apr 2013, 12:56 PM
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
Tags
General Discussions
Asked by
Kevin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Kevin
Top achievements
Rank 1
Share this question
or