locked
Server Control Question RRS feed

  • Question

  • User1051579230 posted

     Hello,

     I'm new to creating server controls so this might be pretty easy!

     
    I'm trying to create a Custom Regex Validator by inheriting from the control RegularExpressionValidator.  I think I have the regex portion working....but I want to customize the Error Message and Text in the Server Control.

     

    Here's what I have so far....

     

    1    Imports System
    2    Imports System.Collections.Generic
    3    Imports System.ComponentModel
    4    Imports System.Text
    5    Imports System.Web
    6    Imports System.Web.UI
    7    Imports System.Web.UI.WebControls
    8    
    9    
    10   <DefaultProperty("Text"), ToolboxData("<{0}:phoneNumberValidator runat=server></{0}:phoneNumberValidator>")> _
    11   Public Class phoneNumberValidator
    12       Inherits RegularExpressionValidator
    13   
    14   
    15       '<Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property Text() As String
    16       '    Get
    17       '        Dim s As String = CStr(ViewState("Text"))
    18       '        If s Is Nothing Then
    19       '            Return "[" + Me.ID + "]"
    20       '        Else
    21       '            Return s
    22       '        End If
    23       '    End Get
    24   
    25       '    Set(ByVal Value As String)
    26       '        ViewState("Text") = Value
    27       '    End Set
    28       'End Property
    29   
    30   
    31       <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Overloads ReadOnly Property ValidationExpression() As String
    32           Get
    33               Return "^\([0-9]{3}\)\s?[0-9]{3}(-|\s)?[0-9]{4}$|^[0-9]{3}-?[0-9]{3}-?[0-9]{4}$"
    34           End Get
    35       End Property
    36   
    37       <Bindable(True), Category("Appearance"), DefaultValue("--This is not a valid phone number!!"), Localizable(True)> Public Overloads ReadOnly Property ErrorMessage() As String
    38           Get
    39               Return "--This is not a valid phone number!!"
    40           End Get
    41       End Property
    42   
    43       <Bindable(True), Category("Appearance"), Localizable(True), DefaultValue("Not Valid")> Public Overloads ReadOnly Property Text() As String
    44           Get
    45               Return "Invalid phone number!!"
    46           End Get
    47       End Property
    48   
    49       Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
    50           output.Write(Text)
    51           output.Write(ErrorMessage)
    52           output.Write(Me.Text)
    53           output.Write(Me.ErrorMessage)
    54       End Sub
    55   
    56   End Class
    57   
    

     

     

     

    Basically I want to create this control and pass it around for other people to use. 

     

    Any suggestions????

     

    Thanks
     

    Thursday, May 8, 2008 2:55 PM

Answers

  • User-16411453 posted

     You have to wrap the [] around the property name

    output.Write( [ValidationBLOCKED EXP])
     

    [Text]

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 9, 2008 7:47 PM