Imports System.Reflection
Public Class GridViewRowExtender
Private _gvRow As GridViewRow
Public Sub New(ByVal gvRow As GridViewRow)
_gvRow = gvRow
End Sub
Public Function GetText(ByVal textBoxId As String) As String
Return CType(_gvRow.FindControl(textBoxId), TextBox).Text
End Function
Public Function GetText(Of T)(ByVal textBoxId As String) As T
Dim s As String = GetText(textBoxId)
Dim result As T
If Not TryParse(Of T)(s, result) Then
result = Nothing
End If
Return result
End Function
Public Function GetChecked(ByVal checkBoxId As String) As Boolean
Return CType(_gvRow.FindControl(checkBoxId), CheckBox).Checked
End Function
Public Function GetSelectedValue(ByVal dropDownListId As String) As String
Return CType(_gvRow.FindControl(dropDownListId), DropDownList).SelectedValue
End Function
Public Function GetSelectedValue(Of T)(ByVal dropDownListId As String) As T
Dim s As String = GetSelectedValue(dropDownListId)
Dim result As T
If Not TryParse(Of T)(s, result) Then
result = Nothing
End If
Return result
End Function
Private Function TryParse(Of T)(ByVal input As String, ByRef value As T) As Boolean
Dim result As Boolean = False
Dim type As Type = GetType(T)
Dim pt As Type() = New Type() {GetType(System.String)}
Dim mi As MethodInfo = type.GetMethod("Parse", pt)
value = Nothing
Try
value = DirectCast((mi.Invoke(Nothing, New Object() {input})), T)
result = True
Catch
End Try
Return result
End Function
End Class