Search code examples
web-parts

Event Names for WebPartZone Verbs


Trying to figure out how to hook into some web part verb events; specifically, I want to know when the user minimizes or restores any web part, and which one. I'm not on a sharepoint platform - strictly asp.net (4) ajax (2).

I call the following code in page load of my web part (my web part inherits UserControl and Implements IWebPart):

Sub test_WebPartZoneVerbEventHandlers()

    Dim i As Integer = 0

    For Each z As WebPartZone In WebPartManager.GetCurrentWebPartManager(Me.Page).Zones
        Dim restoreVerb As WebPartVerb = z.RestoreVerb
        Dim minimizeVerb As WebPartVerb = z.MinimizeVerb
        If Not IsNothing(restoreVerb) Then
            If Not IsNothing(restoreVerb.ClientClickHandler) Then
                testTextArea.InnerHtml = testTextArea.InnerHtml & i & "<br/>" & restoreVerb.ClientClickHandler
            End If

            If Not IsNothing(restoreVerb.ServerClickHandler) Then
                testTextArea.InnerHtml = testTextArea.InnerHtml & i & "<br/>" & restoreVerb.ServerClickHandler().ToString()

            End If
        End If

        If Not IsNothing(minimizeVerb) Then
            If Not IsNothing(minimizeVerb.ClientClickHandler) Then
                testTextArea.InnerHtml = testTextArea.InnerHtml & i & "<br/>" & minimizeVerb.ClientClickHandler
            End If

            If Not IsNothing(minimizeVerb.ServerClickHandler) Then
                testTextArea.InnerHtml = testTextArea.InnerHtml & i & "<br/>" & minimizeVerb.ServerClickHandler().ToString()

            End If

        End If

        i = i + 1
    Next

End Sub

But I don't get any names for the handlers. Here is the output (contents of testTextArea):

0<br/>0<br/>1<br/>1<br/>2<br/>2<br/>0<br/>0<br/>1<br/>1<br/>2<br/>2<br/>

Anyone know why nothing is showing up for the client/server click handlers?


Solution

  • Couldn't figure out what the event names were, and frankly I'm getting fed up with UpdatePanels and thinking about getting rid of them entirely, but I was able to solve my problem by overriding the verbs property on my web part.

    Imports ((myprojectName)).icBase
    Imports System.Web.UI.WebControls.WebParts
    
    ''' <summary>
    ''' Base class for web parts.
    ''' </summary>
    ''' <remarks></remarks>
    Public MustInherit Class icBaseWebPartControl
        Inherits icBaseUserControlAjax
        Implements IWebPart, IWebActionable
    
    
    #Region "IWebPart Properties"
    
        Private _Title As String
        Private _TitleUrl As String
        Private _TitleIconImageUrl As String
        Private _Description As String
        Private _CatalogIconImageUrl As String
    
        Public ReadOnly Property Subtitle As String Implements IWebPart.Subtitle
            Get
                Return Nothing
            End Get
        End Property
        Public Property Title() As String Implements IWebPart.Title
            Get
                Return Me._Title
            End Get
            Set(ByVal value As String)
                Me._Title = value
            End Set
        End Property
        Public Property Description() As String Implements IWebPart.Description
            Get
                Return Me._Description
            End Get
            Set(ByVal value As String)
                Me._Description = value
            End Set
        End Property
    
        Public Property TitleUrl() As String Implements IWebPart.TitleUrl
            Get
                Return Me._TitleUrl
            End Get
            Set(ByVal value As String)
                Me._TitleUrl = value
            End Set
        End Property
        Public Property TitleIconImageUrl() As String Implements IWebPart.TitleIconImageUrl
            Get
                Return Me._TitleIconImageUrl
            End Get
            Set(ByVal value As String)
                Me._TitleIconImageUrl = value
            End Set
        End Property
    
        Public Property CatalogIconImageUrl() As String Implements IWebPart.CatalogIconImageUrl
            Get
                Return Me._CatalogIconImageUrl
            End Get
            Set(ByVal value As String)
                Me._CatalogIconImageUrl = value
            End Set
        End Property
    
    
        ''' <summary>
        ''' Gives us some functionality of a full web part without actually extending WebPart.  (Me is only an IWebPart, not a true WebPart)
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property WebPart() As WebPart
            Get
                Dim wpm As WebPartManager = WebPartManager.GetCurrentWebPartManager(Me.Page)
                Return wpm.WebParts(Me.ID)
            End Get
        End Property
    
    #End Region
    
    
        Public Overridable ReadOnly Property Verbs As System.Web.UI.WebControls.WebParts.WebPartVerbCollection Implements System.Web.UI.WebControls.WebParts.IWebActionable.Verbs
            Get
    
                Dim minimizeVerb As WebPartVerb = New WebPartVerb(ID & "minimizeVerb", AddressOf VerbMinimize, "OnMinimizeClicked('" & Me.ID & "')") With {.Text = "Minimize", .Description = "Minimize this web part"}
                Dim restoreVerb As WebPartVerb = New WebPartVerb(ID & "restoreVerb", AddressOf VerbRestore, "OnRestoreClicked('" & Me.ID & "')") With {.Text = "Restore", .Description = "Restore this web part"}
    
                Dim collection As Collection = New Collection
                Dim returnValue As New WebPartVerbCollection
    
                If Me.WebPart.ChromeState = PartChromeState.Minimized Then
                    collection.Add(restoreVerb)
                    returnValue = New WebPartVerbCollection(collection)
                End If
                If Me.WebPart.ChromeState = PartChromeState.Normal Then
                    collection.Add(minimizeVerb)
                    returnValue = New WebPartVerbCollection(collection)
                End If
                Return returnValue
            End Get
        End Property
    
        Protected Shared Sub VerbRestore(ByVal sender As Object, ByVal e As WebPartEventArgs)
    
            e.WebPart.ChromeState = PartChromeState.Normal
    
        End Sub
    
        Protected Shared Sub VerbMinimize(ByVal sender As Object, ByVal e As WebPartEventArgs)
    
            e.WebPart.ChromeState = PartChromeState.Minimized
    
        End Sub
    End Class
    

    And then I do the following in the client script called by the above:

     // fired when user clicks restore on  any web part 
        function OnRestoreClicked(webPartID) {
            switch (webPartID) {
                case 'ucMyTimekeeping':
                    // don't need to refresh any update panels in this case, because none of the controls in timekeeping are server-side
                    // (unlike files and docs, which have server-side tab containers and tabs)
                    dbLoadTime();
                    break;
                case 'ucMyDocuments':
    
                    //wait a few milliseconds for the server to un-hide the web part  (see icBaseWebPartControl.vb)
                    //hpTODO:  There has GOT to be a better way to do this...  maybe autopostback on the restore link, but not sure how to go about that.
                    setTimeout(function () {
                        HpDocs.UpdatePanel();
                        dbLoadDocs();
                    }, 200);
                    break;
                case 'ucMyFiles':
                    // hpTODO:  same as for ucMyDocuments
                    setTimeout(function () {
                        HpFiles.UpdatePanel();
                        dbLoadFiles();
                    }, 200);
                    break;
                default:
                    __doPostBack(webPartID, '');
    
            }
           //            return false;
        };
    

    And here is what the UpdatePanel() functions look like:

    // do partial postback on the update panel inside the My Files  .ascx
    HpFiles.UpdatePanel = function () {
        var myFiles = $("[id$='_upMyFiles']").attr('id');
        __doPostBack(myFiles);
    };