VB.NET - Code Samples - MREDKJ Textbox Custom Web Control

Handle Maxlength for Multiline ASP.NET Textbox controls

Synopsis:

A Custom Control wrapper around a textbox that ensures the maxlength property works in both singleline and multiline modes.

The Reason:

The maxlength property can set for ASP.net server textbox controls with any textmode. In the case that the textmode is set to multiline the maxlength property will have no effect. In html these render as text and textarea controls. Unfortunately the textarea has no native maxlength property and the asp.net textbox control does not handle maxlength for a textbox with a texmode of multiline. The only way to handle this is with javascript. Javascript does not handle this quite as nicely as the the maxlength property for text fields in html but it is the closest way to simulate the functionality. (The area of its failing is that a character appears and then dissappears if it is past the maxlength.). Listed below are examples showing the code to be added at page load, code for a custom control, and a compiled download of the custom control. For more information on creating Custom Controls go to Creating Custom Controls by Philip Quinn at ASPAlliance.com. If you would just like to use the compiled control download it and add it to the toolbox of your asp.net project.

The Code - Add Attributes Method:

   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If TextBox1.MaxLength > 0 Then
            TextBox1.Attributes.Add("onkeydown", _
                    "this.value = this.value.slice(0," & TextBox1.MaxLength & ");")
            TextBox1.Attributes.Add("onkeyup", _
               "this.value = this.value.slice(0," & TextBox1.MaxLength & ");")


        End If

    End Sub

The Code - Custom Control Method:

   Imports System.ComponentModel
   Imports System.Web.UI

   ")> _
   Public Class MREDKJTextBox
       Inherits System.Web.UI.WebControls.TextBox
       Implements INamingContainer


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
     Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected Overrides Sub AddAttributesToRender(ByVal _
                 writer As System.Web.UI.HtmlTextWriter)
        MyBase.AddAttributesToRender(writer)
        If Me.MaxLength > 0 Then
            writer.AddAttribute("onkeydown", _
               "this.value = this.value.slice(0," & Me.MaxLength & ");")
            writer.AddAttribute("onkeyup", _
               "this.value = this.value.slice(0," & Me.MaxLength & ");")
        End If
    End Sub

    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        MyBase.OnPreRender(e)
    End Sub

End Class


Download the control
About this page: