Custom Composite Control and Validators

Discussion in 'ASP .Net' started by Rick, Jun 2, 2007.

  1. Rick

    Rick Guest

    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
    Rick, Jun 2, 2007
    #1
    1. Advertising

  2. 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
    >
    >
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Jun 4, 2007
    #2
    1. Advertising

  3. Rick

    Rick Guest

    On Jun 4, 5:35 am, Sergey Poberezovskiy
    <> wrote:
    > 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
    Rick, Jun 5, 2007
    #3
  4. 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


    "Rick" wrote:

    > On Jun 4, 5:35 am, Sergey Poberezovskiy
    > <> wrote:
    > > 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
    >
    >
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Jun 6, 2007
    #4
  5. 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


    "Rick" wrote:

    > On Jun 4, 5:35 am, Sergey Poberezovskiy
    > <> wrote:
    > > 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
    >
    >
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Jun 6, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    708
  2. Sriram
    Replies:
    0
    Views:
    110
    Sriram
    Jun 2, 2004
  3. Chad
    Replies:
    0
    Views:
    211
  4. Sriram
    Replies:
    0
    Views:
    111
    Sriram
    Jun 1, 2004
  5. Brad

    Validators in composite control firing prematurely

    Brad, Nov 29, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    189
    Steven Cheng[MSFT]
    Dec 2, 2005
Loading...

Share This Page