Custom CustomValidator: Properties Window Problems

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Custom CustomValidator: Properties Window Problems

    Hello. I'm still rather new to building custom controls, so please be
    gentle. ;)

    What I'd like to be able to do, when validating, is drop my
    CustomServerValidator (see below) and a generic Label control onto the
    page. Then, all I'd have to do is set two properties:
    ControlToValidate and my custom ErrorLabel. The custom control would
    handle the rest at runtime, including sending the appropriate error
    message to the Label tied to ErrorLabel.

    I'd like the ErrorLabel property to act similarly to the
    ControlToValidate property. When I choose ErrorLabel in the Properties
    window, I'd like to see a dropdown filled with a list of objects -- in
    this case, of type Label -- currently available on the form.

    This is where the error occurs. The dropdown works properly, but if I
    choose one of the labels, I get the following error message in Design
    mode:

    ------------------------------------------------------------------------------------------------------------------------
    Error Rendering Control - CustomServerValidator1
    An unhandled exception has occurred.
    Cannot create an object of type 'System.Web.UI.WebControls.Label' from
    its string representation 'Label1' for the 'ErrorLabel' property.
    ------------------------------------------------------------------------------------------------------------------------

    If I change the ErrorLabel property to type String, the error
    disappears, but then I have to enter the Label's name manually.

    How would I go about fixing this?

    Also, one follow-up question.........

    Because the CustomValidator derives from type Label, all of my
    CustomServerValidators show up in the Properties dropdown list as well.
    Once the issue above is fixed, is there any way to further "filter"
    the dropdown to show only *pure* Label controls?

    Thanks in advance!



    Public Class CustomServerValidator
    Inherits System.Web.UI.WebControls.CustomValidator

    Private _ErrorLabel As Label

    <Browsable(True)> _
    Public Property ErrorLabel() As Label
    Get
    Return _ErrorLabel
    End Get
    Set(ByVal value As Label)
    _ErrorLabel = value
    End Set
    End Property

    Public Sub New()

    Me.EnableClientScript = False
    Me.ValidateEmptyText = True

    End Sub

    <...unrelated code removed...>

    End Class

    philaphan80@yahoo.com Guest

  2. Similar Questions and Discussions

    1. Making Custom Control Properties Visible in Visual Studio's Properties Palette
      I am learning how to use the System.ComponentModel class in VB.NET so that I can add my ASP.NET controls to Visual Studio .NET 2003. I have managed...
    2. Problems with empty strings and Properties window
      I need to be able to set a string property in a control to an empty string using the properties window. The problem is that when I set the...
    3. Custom TextBox with custom attributes and properties question
      I have an webform that has a datarepeater on it. In that repeater I'm binding some data and I have put a textbox control in there. On the...
    4. Browser window properties
      Is it possible to alter the browser properties (toolbar, scrollbar, window width and height, etc.) for a page that isn't a pop-up? In other words,...
    5. properties not showing in properties window at design time
      I created a usercontrol using vb but the properties will not show in the properties pane however the properties show up in the intellisence list....
  3. #2

    Default Re: Custom CustomValidator: Properties Window Problems

    > This is where the error occurs. The dropdown works properly, but if I
    > choose one of the labels, I get the following error message in Design
    > mode:
    >
    > ------------------------------------------------------------------------------------------------------------------------
    > Error Rendering Control - CustomServerValidator1
    > An unhandled exception has occurred.
    > Cannot create an object of type 'System.Web.UI.WebControls.Label' from
    > its string representation 'Label1' for the 'ErrorLabel' property.
    > ------------------------------------------------------------------------------------------------------------------------
    Let the property be of type 'String' because in the aspx, it has to be added
    declaratively, something like:

    <my:CustomServerValidator id='...' ControlToValidate='idOfControlToValidate'
    .... />

    Add the following attribute to the property:

    TypeConverter(typeof(System.Web.UI.WebControls.Val idatedControlConverter))


    --
    Happy Hacking,
    Gaurav Vaish | [url]www.mastergaurav.com[/url]
    [url]www.edujinionline.com[/url]
    [url]http://articles.edujinionline.com/webservices[/url]
    -----------------------------------------


    Gaurav Vaish \(www.EdujiniOnline.com\) Guest

  4. #3

    Default Re: Custom CustomValidator: Properties Window Problems

    Thanks for your quick response, Gaurav! I appreciate the help.

    I wasn't able to use your line of code exactly as you supplied it,
    becuase ValidatedControlConverter returns a list of controls on the
    page which can be validated. (I needed a list of controls on the page
    matching type Label.) However, it pointed me in the right direction.

    After poking around Google Groups and the Help file a bit more, I found
    what I was looking for -- CustomIDConverter.

    I created a new class which inherits CustomIDConverter and overrides
    the Filter method. It basically filters out any of the controls ready
    to be listed if they don't match the specified type. At Design time,
    the dropdown in the Properties window now shows only the labels on the
    form. It also excludes my CustomServerValidator, because it's not a
    *true* Label.

    My revised code is below for anyone who might run into a similar
    situation.



    Public Class LabelConverter
    Inherits ControlIDConverter

    Protected Overrides Function FilterControl(ByVal control As
    System.Web.UI.Control) As Boolean

    If Not TypeOf (control) Is Label Then
    Return False
    End If

    Return MyBase.FilterControl(control)

    End Function

    End Class

    Public Class CustomServerValidator
    Inherits System.Web.UI.WebControls.CustomValidator

    Private _ErrorLabel As String

    <Browsable(True), TypeConverter(GetType(LabelConverter))> _
    Public Property ErrorLabel() As String
    Get
    Return _ErrorLabel
    End Get
    Set(ByVal value As String)
    _ErrorLabel = value
    End Set
    End Property

    Public Sub New()

    Me.EnableClientScript = False
    Me.ValidateEmptyText = True

    End Sub

    <...unrelated code removed...>

    End Class

    philaphan80@yahoo.com Guest

  5. #4

    Default Re: Custom CustomValidator: Properties Window Problems

    > I wasn't able to use your line of code exactly as you supplied it,
    > becuase ValidatedControlConverter returns a list of controls on the
    > page which can be validated. (I needed a list of controls on the page
    > matching type Label.) However, it pointed me in the right direction.
    Yes... it will list down all controls that have an attribute
    "ValidationProperty" (System.Web.UI.ValidationPropertyAttribute)
    > After poking around Google Groups and the Help file a bit more, I found
    > what I was looking for -- CustomIDConverter.
    Yes... that's the root of all Control-To-Validate filters.

    > Protected Overrides Function FilterControl(ByVal control As
    > System.Web.UI.Control) As Boolean
    >
    > If Not TypeOf (control) Is Label Then
    > Return False
    > End If
    >
    > Return MyBase.FilterControl(control)
    >
    > End Function

    How about something like this:

    If TypeOf(control) is Label OR TypeOf(control) is CustomServerValidator Then
    Return True
    Else
    Return False
    End If

    Don't call MyBase.FilterControl(control)
    because that will always return "true".


    --
    Happy Hacking,
    Gaurav Vaish | [url]www.mastergaurav.com[/url]
    [url]www.edujinionline.com[/url]
    [url]http://articles.edujinionline.com/webservices[/url]
    -----------------------------------------


    Gaurav Vaish \(www.EdujiniOnline.com\) Guest

  6. #5

    Default Re: Custom CustomValidator: Properties Window Problems

    > How about something like this:
    >
    > If TypeOf(control) is Label OR TypeOf(control) is CustomServerValidator Then
    > Return True
    > Else
    > Return False
    > End If

    No, that wouldn't work, because it would display my own control in the
    dropdown list. The way I listed it above works well, because it
    excludes anything that's not a Label.

    Thanks again for your help, Gaurav. You saved me from a lot of time
    and frustration.

    philaphan80@yahoo.com Guest

  7. #6

    Default Re: Custom CustomValidator: Properties Window Problems

    > No, that wouldn't work, because it would display my own control in the
    > dropdown list. The way I listed it above works well, because it
    > excludes anything that's not a Label.
    Hmm... chicken and egg problem in that case...
    > Thanks again for your help, Gaurav. You saved me from a lot of time
    > and frustration.
    Always welcome!


    --
    Happy Hacking,
    Gaurav Vaish | [url]www.mastergaurav.com[/url]
    [url]www.edujinionline.com[/url]
    [url]http://articles.edujinionline.com/webservices[/url]
    -----------------------------------------


    Gaurav Vaish \(www.EdujiniOnline.com\) Guest

  8. #7

    Default Re: Custom CustomValidator: Properties Window Problems

    > Hmm... chicken and egg problem in that case...

    You're right, Gaurav.

    When I was testing this previously, I only had one
    CustomServerValidator on the page. Once I dropped a second one onto
    the page, it was available to the first one as a possible ErrorLabel
    control. The first was available to the second as well.

    It seems by default, the control filters itself from the list even when
    overridden. But the other ones are fair game. (That's why I never saw
    my control in the dropdown list! It was automatically filtering
    itself.)

    Anyway, I've modified the code and figured it was worth posting here in
    case anyone else runs into a similar problem.


    If TypeOf (control) Is BaseValidator Then
    Return False
    ElseIf Not TypeOf (control) Is Label Then
    Return False
    Else
    Return True
    End If

    philaphan80@yahoo.com Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139