Hi Walter,
Thank you for your reply.
Yes, this is the same custom control. I did not try with composite control
yet. I try to figure out what is the better way to handle all controls we
need in the project. I will need a lot of reading to catch up about composite
control.
I figured out the problem. When I’m working with web form (Visual Studio
2005), webformX.aspx.designer.vb did not get update all the custom controls
until I double click on one of the control then aspx.designer.vb get add all
controls to the page and I can call any control from my code now. I do not
know this is my coding or not. Below is my custom control codes:
'''Base textbox
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace psc
Public Class baseValidator
Inherits TextBox
Implements IValidator
Private _valid As Boolean = True
Private _errorMessage As String = ""
Private _blankAllowed As Boolean = True
Private _friendlyName As String = ""
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
MyBase.OnInit(e)
Page.Validators.Add(Me)
End Sub
Protected Overrides Sub OnUnload(ByVal e As System.EventArgs)
If Not (Page Is Nothing) Then
Page.Validators.Remove(Me)
End If
MyBase.OnUnload(e)
End Sub
Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
MyBase.Render(writer)
End Sub
Public Property IsValid() As Boolean Implements
System.Web.UI.IValidator.IsValid
Get
Return _valid
End Get
Set(ByVal value As Boolean)
_valid = value
If Not _valid Then
Me.BackColor = Drawing.Color.MistyRose
Else
Me.BackColor = Drawing.Color.White
End If
End Set
End Property
Public Property ErrorMessage() As String Implements
System.Web.UI.IValidator.ErrorMessage
Get
Return _errorMessage
End Get
Set(ByVal value As String)
_errorMessage = value
End Set
End Property
Public Property AllowBlank() As Boolean
Get
Return _blankAllowed
End Get
Set(ByVal value As Boolean)
_blankAllowed = value
End Set
End Property
Public Property FriendlyName() As String
Get
Return _friendlyName
End Get
Set(ByVal value As String)
_friendlyName = value
End Set
End Property
Public Overridable Sub Validate() Implements
System.Web.UI.IValidator.Validate
Me.IsValid = True
If Not Me.AllowBlank Then
Dim isBlank As Boolean = IIf(Me.Text.Length = 0, True, False)
If isBlank Then
Me.ErrorMessage = String.Format("'{0}' cannot be
blank.", Me.FriendlyName)
Me.IsValid = False
End If
End If
End Sub
End Class
End Namespace
'''Textbox control for email
Imports System
Imports pscControl
Imports System.Text
Imports System.Text.RegularExpressions
Namespace psc
Public Class emailTextbox
Inherits baseValidator
Private Const formatEamil As String = _
"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
Private _Value As String
Public Property Value() As String
Get
Return _Value
End Get
Set(ByVal value As String)
_Value = value
Me.Text = _Value.ToString
End Set
End Property
Public Overrides Sub Validate()
MyBase.Validate()
Me.IsValid = True
Dim isblank As Boolean = IIf(Me.Text.Length = 0, True, False)
If isblank Then
If AllowBlank = False Then
Me.ErrorMessage = String.Format("'{0}' cannot be
blank.", Me.FriendlyName)
Me.IsValid = False
Exit Sub
End If
Else
Try
_Value = Me.Text
If Not Regex.IsMatch(_Value, formatEamil) Then
Me.ErrorMessage = String.Format("'{0}' must be a
valid email address.", Me.FriendlyName)
Me.IsValid = False
End If
Catch ex As Exception
Me.ErrorMessage = String.Format("'{0}' is not a valid
email address.", Me.FriendlyName)
Me.IsValid = False
End Try
End If
End Sub
End Class
End Namespace
Thank you.
PS. I got sample code from
http://www.codeproject.com/aspnet/selfvalidatingtextbox.asp by Patrick Meyer.
Please let me know if you have any good sample for composite control.