Click here to Skip to main content
15,896,269 members
Articles / Web Development / ASP.NET

Extended ASP.NET GridView with cell click events

Rate me:
Please Sign up or sign in to vote.
4.71/5 (7 votes)
21 Jan 2010CPOL2 min read 77.4K   2K   25  
An extended ASP.NET Gridview control with cell click events.
Imports System
Imports System.Collections.Generic
Imports System.Collections
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


Namespace CustomControls

    <ToolboxData("<{0}:XGridView ID='XGridView' runat=server></{0}:XGridView>")> _
    Public Class XGridView
        Inherits System.Web.UI.WebControls.GridView

#Region "Properties"

        <DefaultValue(False)> _
        Public Property EnableRowClick() As Boolean
            Get
                Dim ret As Boolean = False
                Dim obj As Object = ViewState("EnableRowClick")
                If obj IsNot Nothing Then
                    ret = CBool(obj)
                End If
                Return ret
            End Get
            Set(ByVal value As Boolean)
                ViewState("EnableRowClick") = value
            End Set
        End Property

        <DefaultValue(False)> _
        Public Property EnableCellClick() As Boolean
            Get
                Dim ret As Boolean = False
                Dim obj As Object = ViewState("EnableCellClick")
                If obj IsNot Nothing Then
                    ret = CBool(obj)
                End If
                Return ret
            End Get
            Set(ByVal value As Boolean)
                ViewState("EnableCellClick") = value
            End Set
        End Property

#End Region

#Region "Row Event"


        Private Shared ReadOnly RowClickedEventKey As Object = New Object

        Public Custom Event RowClicked As EventHandler(Of GridViewRowClickedEventArgs)
            AddHandler(ByVal value As EventHandler(Of GridViewRowClickedEventArgs))
                Events.AddHandler(RowClickedEventKey, value)
            End AddHandler

            RemoveHandler(ByVal value As EventHandler(Of GridViewRowClickedEventArgs))
                Events.RemoveHandler(RowClickedEventKey, value)
            End RemoveHandler

            RaiseEvent(ByVal sender As Object, ByVal e As GridViewRowClickedEventArgs)
                Dim ev As EventHandler(Of GridViewRowClickedEventArgs) = TryCast(Events(RowClickedEventKey), EventHandler(Of GridViewRowClickedEventArgs))
                If ev IsNot Nothing Then
                    ev(sender, e)
                End If
            End RaiseEvent
        End Event
        Protected Overridable Sub OnRowClicked(ByVal e As GridViewRowClickedEventArgs)
            RaiseEvent RowClicked(Me, e)
        End Sub

#End Region
#Region "Cell Events"
        Private Shared ReadOnly CellClickedEventKey As Object = New Object

        Public Custom Event CellClicked As EventHandler(Of GridViewCellClickedEventArgs)
            AddHandler(ByVal value As EventHandler(Of GridViewCellClickedEventArgs))
                Events.AddHandler(CellClickedEventKey, value)
            End AddHandler

            RemoveHandler(ByVal value As EventHandler(Of GridViewCellClickedEventArgs))
                Events.RemoveHandler(CellClickedEventKey, value)
            End RemoveHandler

            RaiseEvent(ByVal sender As Object, ByVal e As GridViewCellClickedEventArgs)
                Dim ev As EventHandler(Of GridViewCellClickedEventArgs) = TryCast(Events(CellClickedEventKey), EventHandler(Of GridViewCellClickedEventArgs))
                If ev IsNot Nothing Then
                    ev(sender, e)
                End If
            End RaiseEvent
        End Event

        Protected Overridable Sub OnCellClicked(ByVal e As GridViewCellClickedEventArgs)
            RaiseEvent CellClicked(Me, e)
        End Sub

#End Region



#Region "Postback handling"
        Protected Overrides Sub RaisePostBackEvent(ByVal eventArgument As String)
            If eventArgument.StartsWith("rc") Then
                Dim index As Integer = Int32.Parse(eventArgument.Substring(2))
                Dim args As New GridViewRowClickedEventArgs(Me.Rows(index), index)
                OnRowClicked(args)
            Else
                MyBase.RaisePostBackEvent(eventArgument)
            End If
            If eventArgument.StartsWith("cc") Then

                Dim lcolIndices() As String = eventArgument.Split(";")
                Dim lintRowIndex As Integer = lcolIndices(1).ToString()
                Dim lintColumnIndex As Integer = lcolIndices(2).ToString()
                Dim args As New GridViewCellClickedEventArgs(Me.Rows(lintRowIndex).Cells(lintColumnIndex), Me.Rows(lintRowIndex), lintColumnIndex)
                OnCellClicked(args)
            Else
                MyBase.RaisePostBackEvent(eventArgument)
            End If

        End Sub


#End Region

#Region "Adding the wiring from client-side to server-side, causing the posback when row is clicked"

        Protected Overrides Sub PrepareControlHierarchy()
            MyBase.PrepareControlHierarchy()

            If EnableRowClick Then
                Dim i As Integer
                For i = 0 To Rows.Count - 1
                    Dim argsData As String = "rc" & Rows(i).RowIndex.ToString()
                    Me.Rows(i).Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(Me, argsData))
                    Me.Rows(i).Attributes("onmouseover") = "this.style.cursor='hand';this.style.textDecoration='underline';"
                    Me.Rows(i).Attributes("onmouseout") = "this.style.textDecoration='none';"
                Next
            End If

            If EnableCellClick Then
                For i As Integer = 0 To Rows.Count - 1
                    For j As Integer = 0 To Rows(i).Cells.Count - 1
                        '                                                  Row Index                                'Column Index              
                        Dim argsData As String = "cc" & ";" & Rows(i).RowIndex.ToString & ";" & j.ToString
                        Me.Rows(i).Cells(j).Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(Me, argsData))
                        Me.Rows(i).Cells(j).Attributes("onmouseover") = "this.style.cursor='hand';this.style.textDecoration='underline';"
                        Me.Rows(i).Cells(j).Attributes("onmouseout") = "this.style.textDecoration='none';"
                    Next
                Next

            End If

        End Sub
#End Region




    End Class
#Region "Custom row event argument type"
    Public Class GridViewRowClickedEventArgs
        Inherits EventArgs
        Private mobjRow As GridViewRow
        Private mintRowIndex As Integer
        Public Sub New(ByVal row As GridViewRow, ByVal index As Integer)
            mobjRow = row
            mintRowIndex = index
        End Sub
        Public ReadOnly Property Row() As GridViewRow
            Get
                Return mobjRow
            End Get
        End Property
        Public ReadOnly Property RowIndex() As Integer
            Get
                Return mintRowIndex
            End Get
        End Property
    End Class
#End Region


#Region "Custom cell event argument type"
    Public Class GridViewCellClickedEventArgs
        Inherits EventArgs
        Private mobjCell As TableCell
        Private mobjRow As GridViewRow
        Private mintColumnIndex As Integer
        Public Sub New(ByVal Cell As TableCell, ByVal row As GridViewRow, ByVal ColumnIndex As Integer)
            mobjCell = Cell
            mobjRow = row
            mintColumnIndex = ColumnIndex
        End Sub
        Public ReadOnly Property Cell() As TableCell
            Get
                Return mobjCell
            End Get
        End Property

        Public ReadOnly Property Row() As GridViewRow
            Get
                Return mobjRow
            End Get
        End Property

        Public ReadOnly Property ColumnIndex() As Integer
            Get
                Return mintColumnIndex
            End Get
        End Property

    End Class


#End Region


End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions