This is a migrated thread and some comments may be shown as answers.
XML Datasource: XmlDataSourceNodeDescriptor on ButtonDataBound
12 Answers 73 Views
This is a migrated thread and some comments may be shown as answers.
Paul Mc
Top achievements
Rank 1
Paul Mc asked on 01 Jul 2009, 03:19 PM
Hi

We are trying to hook a toolbar through to an XML file that we are also using for Context Menus on a Rad Tree View as well.

To do this, we cant use the LoadContentFile method because the XML structure we are using is not the same as the expected structure for this control.  Instead, we are populating an XML Data Source with the XML and then binding the Toolbar to the Data Source.

When we trying to evaluate e.Button.DataItem in the ButtonDataBound event, .Net is reporting that the dataItem type is a System.Web.UI.WebControls.XmlDataSourceNodeDescriptor.

I've seen elsewhere in the forums that someone had a similar problem when trying to use LoadContentFile here but im wondering if this is a different spin on the same problem?

For your information, we are currently using Q1 2009 Bulid 527

Below is a cut down sample of what we are trying to do:

XML File
<?xml version="1.0" encoding="utf-8" ?> 
 
<Menu> 
 
  <Reports> 
    <Item Value="Preview" Text="Preview" ImageName="Preview" /> 
    <Item Value="Print" Text="Print" ImageName="Print" /> 
    <Item Value="Export" Text="Export" ImageName="Export">  
      <Item Value="ExportCSV" Text="CSV Format" ImageName="Export"  /> 
      <Item Value="ExportPDF" Text="PDF Format" ImageName="Export" /> 
    </Item> 
 
  </Reports> 
 
</Menu> 

Populate Toolbar
           Public Shared Function GetToolbar(ByVal Type As MenuType) As RadToolBar  
 
                Dim FilePath As String = "~/App_Data/Menus.xml" 
                Dim xPath As String = "/Menu/Reports/Item"
 
                Dim xmlMenu As New XmlDataSource  
                With xmlMenu  
                    .DataFile = FilePath 
                    .XPath = xPath 
                    .DataBind()  
                End With  
 
                tlb = New RadToolBar  
                With tlb  
                    .DataSource = xmlMenu 
                    .DataBind()  
                End With  
 
                Return tlb  
 
            End Function 

Button Data Bound Event
 
            Private Shared Sub tlb_ButtonDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadToolBarButtonEventArgs) Handles tlb.ButtonDataBound  
 
                With e.Button  
 
                    Dim element As XmlElement = DirectCast(.DataItem, XmlElement)  
 
                    If element.HasAttribute("Value") Then .Value = element.Attributes("Value").Value  
                    If element.HasAttribute("Text") Then .Text = element.Attributes("Text").Value
                    If element.HasAttribute("ImageName") Then .ImageUrl = GetImageURL(element.Attributes("ImageName").Value)
 
                End With  
 
            End Sub 

I hope I haven't missed something obvious!

12 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 01 Jul 2009, 03:34 PM
Hi Paul Mc,

I am afraid you have discovered a bug in RadToolBar. Indeed the DataItem property should have been of XmlElement type. As a workaround you can use the DataBinder.Eval method:

    Protected Sub RadToolBar1_ButtonDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadToolBarButtonEventArgs) Handles RadToolBar1.ButtonDataBound
        Dim dataItem = e.Button.DataItem
        e.Button.Text = DataBinder.Eval(dataItem, "Text")
    End Sub

I have logged this bug for fixing and have updated your telerik points.

Regards,
Albert
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Paul Mc
Top achievements
Rank 1
answered on 01 Jul 2009, 03:46 PM
Albert

Thats an extremely fast answer.  Thank you.

Its not a problem at the moment, but is there any way I check to see if the "field" I'm looking does exist in the datasource?

I.e. Replicate the functionality I have for 
    If element.HasAttribute("Value") Then.....
0
Atanas Korchev
Telerik team
answered on 01 Jul 2009, 03:52 PM
Hi Paul Mc,

After further investigation it appears that this is the expected behavior. The built-in DropDownList control behaves in the exactly same way - the DataItem is XmlDataSourceNodeDescriptor. Still you could implement this as a workaround:

    Protected Sub RadToolBar1_ButtonDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadToolBarButtonEventArgs) Handles RadToolBar1.ButtonDataBound
        Dim dataItem = e.Button.DataItem
        Dim properties = TypeDescriptor.GetProperties(dataItem)
        If properties.Find("
Value", True) IsNot Nothing Then

        End If
    End Sub

I apologize for the misleading you about the bug report.

All the best,
Albert
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Paul Mc
Top achievements
Rank 1
answered on 01 Jul 2009, 04:05 PM
Albert

Thanks again for coming back to me so quickly.  I havent tested your last post as yet, but im sure it will work.  However, are you sure that this is desired functionality?  I was looking through the online documentation just before my initial post and found this.

The documentation would imply that what i was originally trying to do is correct, all be it the example in the doc is using a SQL data source I think.
0
Paul Mc
Top achievements
Rank 1
answered on 01 Jul 2009, 04:08 PM
Just as an additional note, we do use exactly the same code for context menus on the Rad Tree View without problems too.
0
Atanas Korchev
Telerik team
answered on 01 Jul 2009, 04:38 PM
Hi Paul Mc,

The context menu is hierarchical control and for such controls the XmlDataSource seems to behave in a different way. When used in "flat" controls the xml datasource returns XmlDataSourceNodeDescriptor instead of XmlElement. Our help topic discusses binding to DataTable where the dataitem is indeed DataRowView but the DataTable is flat as well.
 
Regards,
Albert
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Paul Mc
Top achievements
Rank 1
answered on 01 Jul 2009, 04:55 PM
Fair comment Albert.  Your solution works for me so I'm very happy.

Something that isnt a big issue but you might want to look at, looking again at that help topic for the data table example. The example appears to be incorrect.  It currently reads:

        DataRowView dataSourceRow = (DataRowView) e.Item.DataItem;

and i think it should read

        DataRowView dataSourceRow = (DataRowView) e.button.DataItem;

on the basis that Item is not a valid property in the ButtonDataBound event.

Thanks once again for all your help and yet another speedy reply.

Paul
0
Paul Mc
Top achievements
Rank 1
answered on 01 Jul 2009, 05:22 PM
Albert

Final question today I promise on the basis its home time! :)

In our XML at the top of this thread, we have "CSV format" and "PDF format" menu items as children to the "Export" menu item.  I understand that you saying that the toolbar has no hierachy but you can mimic it is using drop down lists and placing buttons on them.

How would you go about doing that when you are using a "bound" custom XML file?  Is it even possible or will we have to read the XML buttons ourself and then create the buttons/drop downs?

I guess what im sort of trying to do is recognise that this XML row has child elements and then change the button to a drop down accordingly.

Thanks once again.



0
Atanas Korchev
Telerik team
answered on 02 Jul 2009, 08:03 AM
Hi Paul Mc,

I am afraid you have to create the dropdown by hand. RadToolBar currently cannot databind in such way.
By the way I have found a workaround to get the DataItem as XmlElement:

            dataSource.DataFile = "~/XMLFile.xml"
            dataSource.XPath = "/Menu/Reports/Item"
            RadToolBar1.DataSource = dataSource.GetXmlDocument().SelectNodes(dataSource.XPath)
            RadToolBar1.DataBind()

This way it should be easier to create the hierarchy - you can check whether the XmlElement has child nodes.

I hope this helps,
Albert
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Paul Mc
Top achievements
Rank 1
answered on 02 Jul 2009, 08:32 AM

Morning Albert

Thanks for coming back to me.  That works great and from a consistency point of view it would probably be the way I would implement this.  However, without being able to change a button to a drop down on the fly during DataBound or ItemCreated I don't think I can get to where I want to with a bound toolbar.

I think I'm going to have to do it the long way and read the XML myself to create the necessary type of buttons.

Thanks for all you help with this issue, your support has been fantastic!

0
Accepted
Atanas Korchev
Telerik team
answered on 02 Jul 2009, 08:39 AM
Hi Paul Mc,

I've created a sample web site which shows how to use XmlDataSource and create dropdowns on the fly. I hope it will help you to achieve what you want.

Regards,
Albert
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Paul Mc
Top achievements
Rank 1
answered on 02 Jul 2009, 09:15 AM
Albert

What can I say!  Fantastic!

I was too busy looking for e.button.remove or something like that that I never even thought about going back to the toolbar and remove the current button from there.

The perfect solution - thank you.



Asked by
Paul Mc
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Paul Mc
Top achievements
Rank 1
Share this question
or