By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,681 Members | 1,775 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,681 IT Pros & Developers. It's quick & easy.

Custom Composite Control and Validators

P: n/a
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

Jun 2 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

"Rick" wrote:
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

Jun 4 '07 #2

P: n/a
On Jun 4, 5:35 am, Sergey Poberezovskiy
<SergeyPoberezovs...@discussions.microsoft.comwrot e:
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

"Rick" wrote:
Hello,
I built a composite webcontrolthat has a textbox and a date
control.
added mycustomcontrolon a webform where there are other standard
controls.
Eachcontrolon the form has a requiredvalidator.
I need to have avalidatorfor mycustomcontrol, but don't know how
to do this.
I added a CustomValidator to the form, but in design mode, mycustom
controldoesn't appear in the list of controls of thevalidator.
Any help to work around this will be greatly appreciated.
Thanks,
Rick- Hide quoted text -

- 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

Jun 5 '07 #3

P: n/a
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(getParentCo ntrolIDs(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
"Rick" wrote:
On Jun 4, 5:35 am, Sergey Poberezovskiy
<SergeyPoberezovs...@discussions.microsoft.comwrot e:
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

"Rick" wrote:
Hello,
I built a composite webcontrolthat has a textbox and a date
>control.
added mycustomcontrolon a webform where there are other standard
controls.
Eachcontrolon the form has a requiredvalidator.
I need to have avalidatorfor mycustomcontrol, but don't know how
to do this.
I added a CustomValidator to the form, but in design mode, mycustom
>controldoesn't appear in the list of controls of thevalidator.
Any help to work around this will be greatly appreciated.
Thanks,
Rick- Hide quoted text -
- 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

Jun 6 '07 #4

P: n/a
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(getParentCo ntrolIDs(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
"Rick" wrote:
On Jun 4, 5:35 am, Sergey Poberezovskiy
<SergeyPoberezovs...@discussions.microsoft.comwrot e:
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

"Rick" wrote:
Hello,
I built a composite webcontrolthat has a textbox and a date
>control.
added mycustomcontrolon a webform where there are other standard
controls.
Eachcontrolon the form has a requiredvalidator.
I need to have avalidatorfor mycustomcontrol, but don't know how
to do this.
I added a CustomValidator to the form, but in design mode, mycustom
>controldoesn't appear in the list of controls of thevalidator.
Any help to work around this will be greatly appreciated.
Thanks,
Rick- Hide quoted text -
- 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

Jun 6 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.