Custom Composite Control and Validators

R

Rick

Hello,

I built a composite web control that has a textbox and a date
control.
added my custom control on a webform where there are other standard
controls.
Each control on the form has a required validator.
I need to have a validator for my custom control, but don't know how
to do this.
I added a CustomValidator to the form, but in design mode, my custom
control doesn't appear in the list of controls of the validator.
Any help to work around this will be greatly appreciated.

Thanks,

Rick
 
G

Guest

Rick,

the easiest would be to add validators to your custom control and then
expose say Required property which would make the (internal) validator either
Visible or not.

Another way would be to subclass the system custom validator and then
override both ControlToValidate property and AddAttributesToRender methods
(and maybe a few others - I have done this awhile back - do not remember
exactly). Though this will work in the current .Net version - you are very
likely to re-write your custom validator when new version is out or even toss
it as framework may change significantly between versions (as it did from 1.1
to 2.0)

Hope this helps
 
R

Rick

Rick,

the easiest would be to add validators to yourcustomcontroland then
expose say Required property which wouldmakethe (internal)validatoreitherVisibleor not.

Another way would be to subclass the systemcustomvalidatorand then
override both ControlToValidate property and AddAttributesToRender methods
(and maybe a few others - I have done this awhile back - do not remember
exactly). Though this will work in the current .Net version - you are very
likely to re-write yourcustomvalidatorwhen new version is out or even toss
it as framework may change significantly between versions (as it did from 1.1
to 2.0)

Hope this helps






- Show quoted text -

Thanks Sergey. In my case I need to make this custom control work with
an external validator on the form, to be consistent with the other
controls that are placed on the form. The situation now is that the
validator doesn't list my custom control under the list of controls to
validate, so I cannot select it from the list. How can I make the
validator "see" my custom control?

Any help with this will be greatly appreciated. Thanks!

Rick
 
G

Guest

Rick,

In fact, validators only list limited number of controls - and webcontrols
are definitely the ones they do not. The reason is relatively clear - the
internal structure of them is not known and can change.
I have not tested the following code in .Net 2.0, but I had it working in 1.1:
<TypeConverter(GetType(EnumConverter))> _
Public Overrides Property ControlToValidate As String
Get
If Me.Context Is Nothing Then
EnumConverter.defaultEnum = New
EnumConverter.StandardValuesCollection(getParentControlIDs(Me, True))
End If
Return CType(Me.ViewState("ControlToValidate"))
End Get
Set (ByVal Value As String)
Me.ViewState("ControlToValidate") = Value
End Set

then you need to define your converter:
Public Class EnumConverter
Inherits StringConverter
Public Shared defaultEnum As StandardValuesCollection
Public Overloads Overrides Function GetStandardValuesSupported(Byval
context As System.ComponentModel.ITypeDescriptionContext) As Boolean
Return True
End Function
Public Overloads Overrides Function GetStandardValues(ByVal context As
System.ComponentModel.ITypeDescriptionContext) As
System.ComponentModel.StandardValuesCollection
Return defaultEnum
End Function
End Class

then you need to define your function (in the same validator override)

Private Shared Function getParentControlIDs(ByVal control As Web.UI.Control)
as String()
Dim list As ArrayList = New ArrayList
For Each child As Web.Ui.Control In control.NamingContainer.Controls
If Not child.Equals(control) _
AndAlso Not TypeOf child Is BaseValidator _
AndAlso Not TypeOf child Is CustomValidator _
Then
' AndAlso Not TypeOf child Is Label / Literal, Button,
LinkButton, etc. - exclude as many as you think is appropriate
' alternatively you can specify the types of controls that you want
included - no solution is perfect
list.Add(child.ID)
End
Next
Dim result As String() = Ctype(Array.CreateInstance(GetType(String),
list.Count), String())
list.CopyTo(result)
Return result
End Function
 
G

Guest

Rick,

I am pretty sure that I have posted you a reply about half an hour ago - but
since I cannot see it appearing I will try again:

Rick,

In fact, validators only list limited number of controls - and webcontrols
are definitely the ones they do not. The reason is relatively clear - the
internal structure of them is not known and can change.
I have not tested the following code in .Net 2.0, but I had it working in 1.1:
<TypeConverter(GetType(EnumConverter))> _
Public Overrides Property ControlToValidate As String
Get
If Me.Context Is Nothing Then
EnumConverter.defaultEnum = New
EnumConverter.StandardValuesCollection(getParentControlIDs(Me, True))
End If
Return CType(Me.ViewState("ControlToValidate"))
End Get
Set (ByVal Value As String)
Me.ViewState("ControlToValidate") = Value
End Set

then you need to define your converter:
Public Class EnumConverter
Inherits StringConverter
Public Shared defaultEnum As StandardValuesCollection
Public Overloads Overrides Function GetStandardValuesSupported(Byval
context As System.ComponentModel.ITypeDescriptionContext) As Boolean
Return True
End Function
Public Overloads Overrides Function GetStandardValues(ByVal context As
System.ComponentModel.ITypeDescriptionContext) As
System.ComponentModel.StandardValuesCollection
Return defaultEnum
End Function
End Class

then you need to define your function (in the same validator override)

Private Shared Function getParentControlIDs(ByVal control As Web.UI.Control)
as String()
Dim list As ArrayList = New ArrayList
For Each child As Web.Ui.Control In control.NamingContainer.Controls
If Not child.Equals(control) _
AndAlso Not TypeOf child Is BaseValidator _
AndAlso Not TypeOf child Is CustomValidator _
Then
' AndAlso Not TypeOf child Is Label / Literal, Button,
LinkButton, etc. - exclude as many as you think is appropriate
' alternatively you can specify the types of controls that you want
included - no solution is perfect
list.Add(child.ID)
End
Next
Dim result As String() = Ctype(Array.CreateInstance(GetType(String),
list.Count), String())
list.CopyTo(result)
Return result
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top