Wednesday, July 9, 2008

ObjectDataSource, Web Services and DataObject(True)

I was working on an AJAX example using DataGrids and it seemed like using an ObjectDataSource source would simplify things like sorting and paging.  My data was coming from a web service that used complex data types as parameters so it was easiest to create a helper class.  After importing System.ComponentModel, I marked the class as a <DataObject(True)> and decorated the functions with <DataObjectMethod(DataObjectMethodType.Select)> _  which made the ObjectDataSource wizard a breeze.

 

Imports System.ComponentModel

 

' Adding the DataObjectAttribute tells ObjectDataSource

' consumers this is a data source class

<DataObject(True)> _

Public Class MyServiceHelper

 

    <DataObjectMethod(DataObjectMethodType.Select)> _

    Public Function GetDivisions() As MySvc.Division()

        Dim ps As New MySvc.MyServicePort

        Dim psReq As New MySvc.GetDivisionsRequest

        GetDivisions = ps.GetDivisions(psReq)

    End Function

 

    <DataObjectMethod(DataObjectMethodType.Select)> _

    Public Function GetStaffByDivision(ByVal divName As String) As MySvc.Staff()

        Dim ps As New MySvc.MyServicePort

        Dim psReq As New MySvc.GetStaffRequest

        Dim ss As New MySvc.StaffSearch

 

        ss.DivisionDescription = divName

        psReq.SearchInformation = ss

        GetStaffByDivision = ps.GetStaff(psReq)

    End Function

 

End Class

 

After creating the helper, this is all the code that was necessary to have two AJAX grids interact:

 

Partial Class MasterDetail

    Inherits System.Web.UI.Page

 

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

        GridView2.Visible = True

        GridView2.DataBind()

    End Sub

 

    Protected Sub ObjectDataSource2_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles ObjectDataSource2.Selecting

        e.InputParameters("divName") = GridView1.DataKeys(GridView1.SelectedIndex).Value.ToString()

    End Sub

 

    Protected Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PageIndexChanged

        GridView1.SelectedIndex = -1

        GridView2.Visible = False

    End Sub

End Class

 

Thanks to Matt Berseth for his excellent post about AJAX GridViews, detailsView and ModalPopup.

 

No comments: