Wednesday, July 30, 2008

MultiView usage

I had never used one before, but a MultiView seemed like a good fit for a “code-less” report that I was working on:

 

<asp:MultiView ID="MultiView1" runat="server">

    <asp:View ID="mvwApplications" runat="server">

        <asp:GridView ID="gvwAppDetail" runat="server" DataSourceID="odsAppDetail" AllowSorting="true" />

    </asp:View>

    <asp:View ID="mvwServers" runat="server">

        <asp:GridView ID="gvwServers" runat="server" DataSourceID="odsServerDetail" AllowSorting="true" />

    </asp:View>

    <asp:View ID="mvwDatabases" runat="server">

        <asp:GridView ID="gvwDatabases" runat="server" DataSourceID="odsDBDetail" AllowSorting="true" />

    </asp:View>

    <asp:View ID="mvwTools" runat="server">

        <asp:GridView ID="gvwTools" runat="server" DataSourceID="odsToolDetail" AllowSorting="true" />

    </asp:View>

    <asp:View ID="mvwLanguages" runat="server">

        <asp:GridView ID="gvwLanguages" runat="server" DataSourceID="odsLangDetail" AllowSorting="true" />

    </asp:View>

</asp:MultiView>

 

The MultiView is controled using LinkButtons:

 

Protected Sub tab_Command(ByVal sender As Object, ByVal e As CommandEventArgs) 

        MultiView1.ActiveViewIndex = CType(e.CommandArgument, Integer)

        CType(sender, LinkButton).CssClass = "btnwide"

    End Sub

 

 

 

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.