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

advice on how best to put a grid inside a user control

1 Answer 17 Views
Grid
This is a migrated thread and some comments may be shown as answers.
andieje
Top achievements
Rank 1
andieje asked on 09 Jul 2009, 11:09 AM
Hi

I have a grid that i use on several different pages. Naturally i don't want to keep copying the grid formatting and template definition and item data bound events from page to page so i thought i would put it in a reusable control.

The problem i am facing is that the different pages that use the control display different sets of data so the logic in the NeedDataSource event is completely different on the different pages.

Could i do something like bubble up the NeedDatasource event so it is handled by the page that uses the control. That doesnt seem a very nice design. Certainly doesn't seem in keeping with the whole principle of information hiding and the like but it's the only thing i could think of

any other suggestions heartily welcomed!

1 Answer, 1 is accepted

Sort by
0
andieje
Top achievements
Rank 1
answered on 09 Jul 2009, 03:43 PM
So i had a go and it seems to work. However i don't want to find out later down the line that my approach is flawed so i was hoping someone from the telerik team would have a look and see if they can forsee any problems. This is what i did:
1) put radgrid in user control
2) I exposed a public property on the User control called DataSource. This set the datasource property of the radgrid
3) I propagated the NeedDataSource event the user control. The page holding the user control handles the bubbled NeedDataSource event by setting the DataSource property of the user control (which in turn sets the data source property of the rad grid)

Here is pertinent code in the user control
Partial Class Examples_controls_JourneyGrid  
    Inherits System.Web.UI.UserControl  
 
    Public Event BubbleNeedDataSource As Telerik.Web.UI.GridNeedDataSourceEventHandler  
 
    Public WriteOnly Property DataSource() As Object 
        Set(ByVal value As Object)  
            gridJourney.DataSource = value  
        End Set 
    End Property 
      
    Protected Sub OnBubbleNeedDataSource(ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)  
 
        RaiseEvent BubbleNeedDataSource(Me, e)  
    End Sub 
 
    Protected Sub gridJourney_NeedDataSource(ByVal source As ObjectByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles gridJourney.NeedDataSource  
        OnBubbleNeedDataSource(e)  
    End Sub 

Here is the pertinent code in a page containing the user control
    Protected Sub gridJourney_NeedDataSource(ByVal source As ObjectByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles JourneyGrid1.BubbleNeedDataSource  
 
        JourneyGrid1.datasource = collection <=====some data i obtained  
    End Sub 
Tags
Grid
Asked by
andieje
Top achievements
Rank 1
Answers by
andieje
Top achievements
Rank 1
Share this question
or