Groups | Blog | Home
all groups > asp.net webcontrols > march 2007 >

asp.net webcontrols : Custom TextBox with validating



Madison
3/13/2007 9:45:21 AM
Hello,

I’m working with large web application project. The application will have a
lot of data entry and need to validate before saving to database. I think if
I build custom textbox for each data type such as integer, currency, email,
phone number, date, and so on with validation. It will help to deduce a lot
of checking, validate and less number of textbox on the form. I finished the
custom control and place in the form I got this message from Error List
(Warning) Generation of designer file failed: Unknown server tag
'psc:emailTextbox' when I browse the page it works fine the way I want. But I
can not call any of custom textbox control in the coding at all. What step
should I take to collect this error? Any ideas?

Thank you
wawang NO[at]SPAM online.microsoft.com
3/14/2007 12:00:00 AM
Hi Madison,

Glad to work with you again.

Is this custom control you mentioned like the control we discussed in your
other post "Custom Server Control"? As I suggested, it's recommended to
create a composite control which contains a TextBox and a Validator control
to achieve your objective.

Regarding this post's error message you're experiencing, would you please
also post the complete code of your control so that we can clearly see what
went wrong? Thanks.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Madison
3/14/2007 8:10:15 AM
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.


wawang NO[at]SPAM online.microsoft.com
3/15/2007 11:24:06 AM
Hi Madison,

I'm not sure if you've found the cause of this issue. Just a kind reminder:
when you're using a custom control, you either need to register the control
in web.config or in the webform. For example, in web.config:

<system.web>
<pages>
<controls>
<add assembly="psc" namespace="psc" tagPrefix="psc" />
</controls>
</pages>
</system.web>

In WebForm:

<%@ Register TagPrefix="psc" Namespace="psc" Assembly="psc" %>


If the custom control are in your App_Code folder and you're using the
default web site mode, you need to use "__code" as the assembly name.

I just tried your code and it seems working fine at my side. Please feel
free to let me know if there's anything I've misunderstood.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
wawang NO[at]SPAM online.microsoft.com
3/18/2007 4:00:08 PM
Hi Madison,

How's it going? Please feel free to let me know if there's anything I can
help.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Madison
3/21/2007 2:42:00 PM
Hi Walter,

Thank you for your follow up.
I end up create 2 custom textbox controls to compare them. I might use
composite control for the production project. It give me more open eyes to
see how much I can do with them.
The next step I will try to create the other controls (dropdownlist, data
binding). Any recommendation?

[quoted text, click to view]
wawang NO[at]SPAM online.microsoft.com
3/22/2007 12:00:00 AM
Hi Madison,

Thanks for the update.

Sorry I missed the question in your previous message regarding "good
examples on composite control", I think Dino's
(http://msdn2.microsoft.com/en-us/asp.net/aa336534.aspx) "A Crash Course on
ASP.NET Control Development: Building Composite Controls"
(http://msdn2.microsoft.com/en-us/library/aa479016.aspx) is quite good. The
composite control is actually not difficult to create, just follow the
patterns in the article and you will be fine.

Dino's <<Programming ASP.NET 2.0>> also has chapters dedicated to creating
custom server controls. Nikhil Kothari and Vandana Datje also have a book
<<Developing Microsoft ASP.NET Server Controls and Components>> which is
quite good, though it's for ASP.NET 1.1.

I'm not sure about your last question about "create the other controls",
could you elaborate? Thanks.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Madison
3/22/2007 9:29:25 AM
Hi,
I said "create the other controls" mean
- dropdownlist - populate list from database and allow users to pick option
and do something with their selection.
- GridView, DataList, Repeater, or something like DataGrid in asp.net 1
because I have to deal with database to add, edit, and view information. I
want the web page will be consistant the way it works.

Thank you for your advice.

[quoted text, click to view]
Madison
3/22/2007 9:44:07 AM
Hi Walter,
I'm a big fan of VB if you could point to the VB samples. Thank you.

[quoted text, click to view]
wawang NO[at]SPAM online.microsoft.com
3/26/2007 2:22:12 AM
Hi Madison,

It looks like you're doing lots of data related work in ASP.NET 2.0. I
think you might find following ASP.NET 2.0 Data Tutorials useful:

http://www.asp.net/learn/dataaccess/default.aspx?tabid=63

All the samples there are both in VB.NET and C#.

Please feel free to let me know if there's anything else I can help.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
wawang NO[at]SPAM online.microsoft.com
3/28/2007 10:19:57 AM
Hi Madison,

Do you have further questions in this post?


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button