North West Business Intelligence Ltd North West Business Intelligence Ltd

Business Intelligence Consultancy

 
  • Increase font size
  • Default font size
  • Decrease font size
Home Technical Tips ASP.NET - Gridview RowCommand event from DropDownList

ASP.NET - Gridview RowCommand event from DropDownList

E-mail Print PDF

I've been doing some ad-hoc coding recently just to rustle up a few reporting/maintenance web pages for a team at a client site to manage data in a table in a nice friendly fashion rather than opening the table in SQL Server Enterprise Manager and editing live!

Hit the usual problems with GridViews and other controls in that they never seem to quite do exactly what you want.  But with the beauty of inheritance, why not make them do what you want instead.  I shamelessly borrowed some code from here but being a casual geek, my language of choice is the ever handy VB.NET so a little conversion was required.

So if you're looking for a VB.NET DropDownList control that bubbles up the right event to trigger the RowCommand event handler of a GridView then feel free to use below.  It's not really my code anyway.

Imports System.ComponentModel
Namespace NWBI.Controls
    Public Class CommandDropDown
        Inherits DropDownList

        <DefaultValue("")> _
        Public Property CommandName() As String
            Get
                If IsNothing(ViewState("CommandName")) Then
                    Return String.Empty
                Else
                    Return DirectCast(ViewState("CommandName"), String)
                End If
            End Get
            Set(ByVal value As String)
                ViewState("CommandName") = value
            End Set
        End Property

        <DefaultValue("")> _
        Public Property CommandArgument() As String
            Get
                If IsNothing(ViewState("CommandArgument")) Then
                    Return String.Empty
                Else
                    Return DirectCast(ViewState("CommandArgument"), String)
                End If
            End Get
            Set(ByVal value As String)
                ViewState("CommandArgument") = value
            End Set
        End Property

        Protected ReadOnly EventCommand As Object = New Object()

        Public Custom Event Command As CommandEventHandler
            AddHandler(ByVal value As CommandEventHandler)
                Events.AddHandler(EventCommand, value)
            End AddHandler

            RemoveHandler(ByVal value As CommandEventHandler)
                Events.RemoveHandler(EventCommand, value)
            End RemoveHandler

            RaiseEvent(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
                MyBase.RaiseBubbleEvent(sender, e)
            End RaiseEvent
        End Event

        Protected Overridable Sub OnCommand(ByVal ce As CommandEventArgs)
            RaiseEvent Command(Me, ce)
        End Sub

        Protected Overloads Overrides Sub OnSelectedIndexChanged(ByVal e As EventArgs)
            MyBase.OnSelectedIndexChanged(e)
            If AutoPostBack Then
                Dim args As New CommandEventArgs(Me.CommandName, Me.CommandArgument)
                OnCommand(args)
            End If
        End Sub

    End Class
End Namespace
Last Updated ( Thursday, 23 July 2009 10:53 )