ASP.NET
  Home arrow ASP.NET arrow Page 3 - Extending the ASP.NET TextField User Contr...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Extending the ASP.NET TextField User Control
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 17
    2007-03-21

    Table of Contents:
  • Extending the ASP.NET TextField User Control
  • Developing your own flexible NumericTextbox control: the beginning
  • Developing your own flexible NumericTextbox control: coding events
  • Developing your own flexible NumericTextbox control: properties to retrieve values

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Extending the ASP.NET TextField User Control - Developing your own flexible NumericTextbox control: coding events


    (Page 3 of 4 )

    I am currently using Page Events to simply maintain ViewState. The code is as follows:

    #Region "Page Events"

      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
    If IsPostBack Then
         
    ErrorText = ViewState.Item("ErrorText").ToString
         
    Required = CType(ViewState.Item("Required"), Boolean)
         
    DecimalPlaces = CType(ViewState.Item("DecimalPlaces"), Integer)
         
    IncludeThousandsSeparator = CType(ViewState.Item("IncludeThousandsSeparator"), Boolean)
         
    RegExpValString = ViewState.Item("RegExp").ToString
         
    MaxValue = CType(ViewState.Item("MaxValue"), Decimal)
         
    MinValue = CType(ViewState.Item("MinValue"), Decimal)
       
    Else
         
    Me.lblInfo.Visible = False
       
    End If
     
    End Sub

      Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
       
    ViewState.Item("ErrorText") = ErrorText
       
    ViewState.Item("Required") = Required
       
    ViewState.Item("DecimalPlaces") = DecimalPlaces
       
    ViewState.Item("IncludeThousandsSeparator") = IncludeThousandsSeparator
       
    ViewState.Item("RegExp") = RegExpValString
       
    ViewState.Item("MaxValue") = MaxValue
       
    ViewState.Item("MinValue") = MinValue
     
    End Sub

    #End Region

    The importance of "lblInfo" is worth mentioning at this moment. When a user control simply contains another user control as part of the design, it doesn't show up properly during design time (on ASPX). All the nested user controls look hidden, and the user would not be happy with an empty control. That's why I added "lblInfo" to show at design time that it is a numeric user control. At runtime it gets automatically hidden.

    Apart from the events, I used a Focus method. When the control receives the focus, the cursor should be available in the text box. If the control is in read-only mode, it will not be able to receive any focus. It is defined as follows:

    Public Overrides Sub Focus()
     
    If Editable Then Me.utxtControl.Focus()
    End Sub

    You can observe that I am calling the focus method of "utxtControl" when this control receives the focus. The validation based on Max and Min values is handled as follows:

    Protected Sub cstmValidator_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cstmValidator.ServerValidate
     
    Dim value As Decimal = UnFormattedValue(args.Value)
     
    If value > MaxValue Or value < MinValue Then
       
    args.IsValid = False
       
    cstmValidator.ErrorMessage = Me.ErrorText & " should be in between " & Me.MinValue & " and " & Me.MaxValue
     
    Else
       
    args.IsValid = True
     
    End If
    End Sub

    Developing your own flexible NumericTextbox control: dealing with Regular Expressions

    Our control needs to deal with Regular Expressions as we would like to have support for decimal places, commas and so forth. The regular expressions are applied as follows based on the properties set by the user:

    Private Sub ApplyRegExpValString()
     
    If IncludeThousandsSeparator Then
       
    If DecimalPlaces > 0 Then
         
    RegExpValString = "(?n:(^$?(?!0,?d)d{1,3}(?=(?<1>,)|(?<1>))(k<1>d{3})*(.d{0," & DecimalPlaces & "})?)$)"
       
    Else
         
    RegExpValString = "(?n:(^$?(?!0,?d)d{1,3}(?=(?<1>,)|(?<1>))(k<1>d{3})*)$)"
       
    End If
     
    Else
       
    If DecimalPlaces > 0 Then
         
    RegExpValString = "^d+(?:.d{0," & DecimalPlaces & "})?$"
       
    Else
         
    RegExpValString = "^d+?$"
       
    End If
     
    End If
    End Sub

    Private Property RegExpValString() As String
     
    Get
       
    Return m_strRegExp
     
    End Get
     
    Set(ByVal value As String)
       
    m_strRegExp = value
       
    Me.regExpValidator.ValidationExpression = value
     
    End Set
    End Property

    A few of the regular expressions which are most commonly used for these types of scenarios are as follows

    • (?n:(^$?(?!0,?d)d{1,3}(?=(?<1>,)|(?<1>))(k<1>d{3})*(.dd)?)$) - for $,comma,compulsory 2 dig dec
    • (?n:(^$?(?!0,?d)d{1,3}(?=(?<1>,)|(?<1>))(k<1>d{3})*)$) - for $, comma, no dig dec
    • (?n:(^$?(?!0,?d)d{1,3}(?=(?<1>,)|(?<1>))(k<1>d{3})*(.d{0,2})?)$) - for $, comma, option 2 dig dec
    • ^d+(?:.d{0,2})?$ - without $, no comma, optional 2 dig dec
    • ^d+?$ - without $, no comma, no dec

    More ASP.NET Articles
    More By Jagadish Chaterjee


       · Hello guys,This is an extension to my previous contribution focusing on...
     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT