Sometimes you need to extend controls. One way is to add properties to a control. I wanted to use a checkbox in a item template in a gridview. I wanted the checkbox to fire an event similar to the row command event. Unfortunately the checkbox does not fire a rowcommand event or has a commandname/command argument.
The solution I came up with was to a add an extended checkbox with a commandargument property. This is straight forward. Just include this in your App_Code folder.
Public Class GridviewCheckBox
Inherits CheckBox
Private _CommandArgument As String
Property CommandArgument() As String
Get
Return _CommandArgument
End Get
Set(ByVal value As String)
_CommandArgument = value
End Set
End Property
Protected Overrides Function SaveViewState() As Object
Dim arrState(2) As Object
arrState(0) = MyBase.SaveViewState()
arrState(1) = Me.CommandArgument 'that is my custom property to be saved
Return arrState
End Function
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
If Not savedState Is Nothing Then
Dim arrState() As Object = savedState
Me.CommandArgument = CType(arrState(1), String)
MyBase.LoadViewState(arrState(0))
End If
End Sub
End Class
In order for your property to be persisted across postback you need to override the LoadViewState and SaveViewState functions.
To use the control in your page register it at the top of the page (or in the web.config)
<%@ Register Namespace="CustomControl" TagPrefix="cc" %>
This can now be placed in the item template of a gridview and fire off an event when checked
<cc:GridviewCheckBox runat="server" ID="chkCheckList" AutoPostBack="true"OnCheckedChanged="chkCheckList_CheckedChanged" Checked='<%# eval("deleteflag") %>' CommandArgument='<%# eval("CheckListID") %>'/>
This can be handled in the code behind and you can use Naming container properties of the checkbox to access the parent gridviewrow and gridview to get the datakey values.
Protected Sub chkCheckList_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim chk As CustomControl.GridviewCheckBox = CType(sender, CustomControl.GridviewCheckBox)
Dim wsBW As New wsBusinesswise.BusinessWise
Dim CheckListID As Integer
Dim row As GridViewRow = CType(chk.NamingContainer, GridViewRow)
Dim gvw As GridView = CType(row.NamingContainer, GridView)
CheckListID = gvw.DataKeys(row.RowIndex).Values("CheckListID")
End Sub
Tuesday, 21 April 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment