Developing a Wonderful ASP.NET TextField Control - Developing your own flexible Textbox control with new features: the skeleton
(Page 3 of 4 )
Once the design is completed, we need to start coding. In the code behind, I followed the skeleton shown below:
Imports System.ComponentModel
<ControlValueProperty("Text"), _
ValidationProperty("Text")> _
Partial Class uctTextField
Inherits System.Web.UI.UserControl
.
.
#Region "Page Events"
.
.
Public Overrides Sub Focus()
Public Property Text() As String
Public Property MaxLength() As Integer
Public Property ErrorText() As String
Public Property Editable() As Boolean
Public Property Required() As Boolean
Public Property LabelTextValue() As String
Public Property LabelTextAlign() As
Public Property LabelTextVisible() As Boolean
Public Property LabelTextWidth() As System.Web.UI.WebControls.Unit
Public Property AutoPostBack() As Boolean
Public Property CssClassTextBoxControl() As String
Public Property CssClassLabelControl() As String
Public Property CssClassLabelText() As String
Public Property Tooltip() As String
Public Property ValidationGroup() As String
Public Property HintMessage() As String
Public Property HintMessageVisible() As Boolean
Public Event TextChanged As EventHandler
.
.
End Class
I removed the code for clarity. Based on the discussion of the requirements in previous sections, I came up with the above list of properties, methods and events. You can modify the properties according to your naming conventions and standards. My intention is just to give you an idea of the list of features of the user control.
Developing your own flexible Textbox control with new features: coding events
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
Me.ErrorText = ViewState("ErrorText")
Required = ViewState("Required")
End If
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreRender
ViewState("ErrorText") = ErrorText
ViewState("Required") = Required
End Sub
#End Region
The only method I used is the 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 txtControl.Focus()
End Sub
Our control should also be supporting TextChanged event, and I created it as follows:
#Region "Events"
Public Event TextChanged As EventHandler
Protected Sub txtControl_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txtControl.TextChanged
Me.Text = txtControl.Text
RaiseEvent TextChanged(sender, e)
End Sub
#End Region
Next: Developing your own flexible Textbox control with new features: coding properties >>
More ASP.NET Articles
More By Jagadish Chaterjee