D
dan_williams
Hi,
I'm trying to create my own custom control based on the textbox, but
with additional properties to specify whether or not the textbox is a
required field, and if its needs to be validated against a regular
expression. I'd also like it to change its backcolor and be focused on
if it's invalid.
Here is my current VB code:-
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class CustomTextBox
Inherits TextBox
Private req As RequiredFieldValidator
Private reg As RegularExpressionValidator
Dim _bgErrorColor As System.Drawing.Color
Dim _required As Boolean
Dim _regExp As String
Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
req = New RequiredFieldValidator
req.ControlToValidate = Me.ID
req.ErrorMessage = "Value required for " & Me.ID
req.Text = "*"
reg = New RegularExpressionValidator
reg.ControlToValidate = Me.ID
reg.ErrorMessage = "Invalid value for " & Me.ID
reg.Text = "*"
If Me.RegExp <> "" Then reg.ValidationExpression = Me.RegExp
Controls.Add(req)
Controls.Add(reg)
End Sub
Protected Overloads Overrides Sub Render(ByVal w As HtmlTextWriter)
MyBase.Render(w)
If Me.Required Then req.RenderControl(w)
If Me.RegExp <> "" Then reg.RenderControl(w)
End Sub
<Bindable(True), Category("Appearance"), DefaultValue("")> Property
[Required]() As Boolean
Get
Return _required
End Get
Set(ByVal Value As Boolean)
_required = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("")> Property
[RegExp]() As String
Get
Return _regExp
End Get
Set(ByVal Value As String)
_regExp = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("Red")>
Property [BgErrorColor]() As System.Drawing.Color
Get
Return _bgErrorColor
End Get
Set(ByVal Value As System.Drawing.Color)
_bgErrorColor = Value
End Set
End Property
End Class
It seems to half work, but my required field validator does not appear
to the immediate right of my textbox, and if I put a validation summary
on my page, it shows the error message regardless of whether or not
i've specified the Required property of my control to true or false.
Does anyone know how i can get this to work, aswell as adding event
handlers or methods to set the focus to my control and make its
background color set to whatever i specify in my BgErrorColor property,
if the control is invalid?
Many thanks in advance for any suggestions
Dan Williams
I'm trying to create my own custom control based on the textbox, but
with additional properties to specify whether or not the textbox is a
required field, and if its needs to be validated against a regular
expression. I'd also like it to change its backcolor and be focused on
if it's invalid.
Here is my current VB code:-
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class CustomTextBox
Inherits TextBox
Private req As RequiredFieldValidator
Private reg As RegularExpressionValidator
Dim _bgErrorColor As System.Drawing.Color
Dim _required As Boolean
Dim _regExp As String
Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
req = New RequiredFieldValidator
req.ControlToValidate = Me.ID
req.ErrorMessage = "Value required for " & Me.ID
req.Text = "*"
reg = New RegularExpressionValidator
reg.ControlToValidate = Me.ID
reg.ErrorMessage = "Invalid value for " & Me.ID
reg.Text = "*"
If Me.RegExp <> "" Then reg.ValidationExpression = Me.RegExp
Controls.Add(req)
Controls.Add(reg)
End Sub
Protected Overloads Overrides Sub Render(ByVal w As HtmlTextWriter)
MyBase.Render(w)
If Me.Required Then req.RenderControl(w)
If Me.RegExp <> "" Then reg.RenderControl(w)
End Sub
<Bindable(True), Category("Appearance"), DefaultValue("")> Property
[Required]() As Boolean
Get
Return _required
End Get
Set(ByVal Value As Boolean)
_required = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("")> Property
[RegExp]() As String
Get
Return _regExp
End Get
Set(ByVal Value As String)
_regExp = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("Red")>
Property [BgErrorColor]() As System.Drawing.Color
Get
Return _bgErrorColor
End Get
Set(ByVal Value As System.Drawing.Color)
_bgErrorColor = Value
End Set
End Property
End Class
It seems to half work, but my required field validator does not appear
to the immediate right of my textbox, and if I put a validation summary
on my page, it shows the error message regardless of whether or not
i've specified the Required property of my control to true or false.
Does anyone know how i can get this to work, aswell as adding event
handlers or methods to set the focus to my control and make its
background color set to whatever i specify in my BgErrorColor property,
if the control is invalid?
Many thanks in advance for any suggestions
Dan Williams