Hi,
NOTE: Source code below...
I build a custom web control library (NO USER CONTROL)
For example, i have a TEditWebControl, and this is a composite control,
with:
- 1 EditBox
- 1 Label
- n Validators
No problem in build the composite control with the EditBox a Label
combination,... but i have troubles with the validators...
I need ad to this custom from 0 to n validators to him (this is setup in
runtime mode), but i don't found the way to bound the validator to the
control. The validator requiered the ControlToValidate property, and this is
a string with the name of the control. But when i try to get in runtime the
ID from the custom control, this is empty, and is empty the id of the inner
controls. I try to set a custom id (and this work), but the validator is no
bound (not found the control).
The code with troubles is this:
Dim oVal As New RequiredFieldValidator()
With oVal
.ErrorMessage = "!"
.ControlToValidate = NombreControlValidar()
'ADD TO THE CONTROL
Me.Controls.Add(oVal) With this way, the validator control is bound, but
when i post the page, the JScrip code send me a error "PageValidators" is
not definen
'ADD TO THE PAGE
Me.Page.Controls.Add(oVal) with this, the validator NOT found the control
'THE METHOD NombreControlValidar() RETURN THE ID FROM THE CONTROL
End With
hOW FIX IT?
(vs.net Beta 2, Win XP Pro)
This is the code:
This is the base class:
'Clase base para todos los controles compuestos de G y G
Public MustInherit Class TGyGWebControl
Inherits System.Web.UI.WebControls.WebControl
Implements INamingContainer
Protected _Label As Label
Private _Requerido As Boolean
Public Sub AgregarValidacion(ByVal Tipo As EnumTipoValidacion, Optional
ByVal Valor As String = "")
'Validar el control en base a las validaciones
'Convertir de fox a .NET si es el caso
'Dim oVal As New TValidacion()
'Convertir la funcion de fox a .NET
EnsureChildControls() 'Llamar siempre antes de accesar controles
Select Case Tipo
'El campo es requerido
Case EnumTipoValidacion.gtvRequerido
Required = True
'Agregar el control de validacion
Dim oVal As New RequiredFieldValidator()
With oVal
.ErrorMessage = "!"
.ControlToValidate = NombreControlValidar()
'Corregir el sistema para que el validador funcione
'Me.Controls.Add(oVal)
End With
'El campo es de fecha
Case EnumTipoValidacion.gtvFecha
'Para que el usuario sepa como debe ingresar...
_Label.Text = _Label.Text + " (d/m/y)"
End Select
End Sub
Public Property Required() As Boolean
Get
EnsureChildControls() 'Llamar siempre antes de accesar controles
Return _Requerido
End Get
Set(ByVal Value As Boolean)
_Requerido = Value
EnsureChildControls() 'Llamar siempre antes de accesar controles
If _Requerido Then
_Label.Font.Bold = True
Else
_Label.Font.Bold = False
End If
End Set
End Property
Public Property Caption() As String
Get
EnsureChildControls() 'Llamar siempre antes de accesar controles
Return _Label.Text
End Get
Set(ByVal Value As String)
EnsureChildControls() 'Llamar siempre antes de accesar controles
_Label.Text = Value
End Set
End Property
Protected Overrides Sub OnPreRender(ByVal E As EventArgs)
'Inicializar los valores
EnsureChildControls()
End Sub
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
'Renderizar los controles
If HasControls() Then
RenderChildren(output)
End If
End Sub
'Funcionalidad abstracta=Debe ser implementada en los controles especificos
Public MustOverride Function NombreControlValidar() As String
Public MustOverride Property MaxLength() As Integer
End Class
This is the implementation class
runat=server>{0}:TEditWebControl>")> Public Class TEditWebControl
Inherits GyGWebControls.TGyGWebControl
Dim _Edit As TextBox
Public ReadOnly Property Control() As WebControl
Get
Return _Edit
End Get
End Property
Protected Overrides Sub CreateChildControls()
'Crear los sub-controles
_Label = New Label()
_Edit = New TextBox()
_Label.Text = "Etiqueta"
_Edit.Text = ""
_Label.Visible = True
_Edit.Visible = True
Controls.Add(_Label)
'Colocar el edit debajo del label
Controls.Add(New LiteralControl(" "))
Controls.Add(_Edit)
_Edit.ID = Me.ID + "Etiqueta"
'Colocar el edit debajo del label
Controls.Add(New LiteralControl("
"))
End Sub
Public Overrides Function NombreControlValidar() As String
EnsureChildControls() 'Llamar siempre antes de accesar controles
Return _Edit.ID
End Function
Public Overrides Property MaxLength() As Integer
Get
EnsureChildControls() 'Llamar siempre antes de accesar controles
Return _Edit.MaxLength
End Get
Set(ByVal Value As Integer)
EnsureChildControls() 'Llamar siempre antes de accesar controles
_Edit.MaxLength = Value
End Set
End Property
Property [Text]()
As String
Get
EnsureChildControls() 'Llamar siempre antes de accesar controles
Return _Edit.Text
End Get
Set(ByVal Value As String)
EnsureChildControls() 'Llamar siempre antes de accesar controles
_Edit.Text = Value
End Set
End Property
End Class
The Life is Beautiful!
Programmer in
Delphi, VS.NET
MCP