Search code examples
asp.netvb.netlinqdatasource

Multiple ascx (user controls) that use the same server side code


I currently have multiple user controls in a default page. Each one has a different grid that is going to become a dashboard. However to cut down on repeating code, some of the controls use the same datasource. Is there anyway to have them all share the same datasource. Or if necessary all use the same OnSelecting statement that I could define elsewhere?

Such as one of the LinqDataSources selects all the users in a group, this displays them all in a gridview. However a detailed gridview below that in a separate ascx control uses the same data, but displays it in another way, grouping differently etc. However they both (if placed on one page) use the same datasource. If there anyway for them to use the same selecting Sub somehow? Maybe part of a class somewhere else in the code?


Solution

  • Ok after further research into alternate ways I've decided to place my select on the default page:

    Public Function returnGroup() As List(Of Group)
        'Use standard LINQ to SQL function to get list of what is needed
    End Function
    

    This has been used in combination with the Structure:

    Structure Group
        Public Property Title As String : Public Property GroupID As Integer
        Public Property GroupCode As String 
    End Structure
    

    So when combined it creates:

    Public Function returnGroup() As List(Of Group)
        'Use standard LINQ to SQL function to get list of what is needed
        Return LINQToSQL_Select.Select(Function(chX) New Group With {.Title = chX.Title} '... etc
    End Function
    

    This is then called in the ascx page:

    Protected Sub Page_Load(ByVAl sender as object, ByVal e as eventargs) Handles Me.Load
        Dim cDef as New _Default
        gvGroup.DataSource = cDef.returnGroup() : gvGroup.DataBind()
    End Sub