jueves, 28 de agosto de 2008

Factory: Devolver valor de un control de cualquier tipo

Private Function tomarValorFactory(ByVal nombreControl As String) As String
If nombreControl IsNot Nothing And nombreControl <> "" Then
Dim control As Control = Me.Parent.FindControl(nombreControl)

Select Case control.GetType.Name
Case "HiddenField"
Return CType(control, HiddenField).Value
Case "TextBox"
Return CType(control, TextBox).Text
Case "Label"
Return CType(control, Label).Text
Case "DropDownList"
Return CType(control, DropDownList).SelectedValue
Case "CheckBox"
Return CType(control, CheckBox).Checked
Case "RadioButton"
Return CType(control, RadioButton).Text
Case "Literal"
Return CType(control, Literal).Text
Case "Calendar"
Return CType(control, Calendar).SelectedDate.ToShortDateString()
Case Else
Throw New Exception("Falta definir el control")
Return ""
End Select
Else
Return ""
End If
End Function