Click here to Skip to main content
15,913,316 members
Articles / Web Development / ASP.NET
Article

Creating Custom Web Controls

Rate me:
Please Sign up or sign in to vote.
1.28/5 (13 votes)
13 Apr 20041 min read 83.4K   1.5K   23   6
This article explains how to extend the base functionality of standard web controls.

Sample screenshot

Introduction

This article explains and illustrates how the standard functionality provided by the Web Controls can be extended in order to fit the requirements of an individual or enterprise.

Background

I was working on a project that deals with the migration of an existing Power Builder application to .NET. Our natural choice for the new platform was .NET, the reasons being availability of developers and overall enterprise move towards a universal Microsoft shop. I came across a functionality in the existing app (Client/Server) where the user double clicks on a text box (date field) and the current date is inserted. In order to replicate this piece of functionality, we had write some JavaScript snippets in each page. Then we thought it is a painful process and hence we decided to come up with a control that extends the existing functionality of a textbox. It could be done with a user control as well, but we thought it might be useful to come up with a Web Control instead, so that this can be used in other projects as well.

Using the code

Download the source code. The download zip file contains a solution with the source code for both the control as well as the sample aspx page that includes the control. The code is given below:

VenusDateTextBox.vb

VB
Option Strict On

Imports System.ComponentModel
Imports System.Web.UI

<DEFAULTPROPERTY("Text"), VALIDATIONPROPERTY("Text"), 
    TOOLBOXDATA("<{0}:VenusDateTextBox runat=server>")> 
 Public Class VenusDateTextBox _
    Inherits System.Web.UI.WebControls.TextBox _
    Implements INamingContainer

#Region " Member Variables "
     Private Const DateIncludeScriptKey As String = "DateCurrentDateInsertScript"

    ' The script block that is rendered to insert the Private I Date.
    Private Const DateCurrentDateInsertScript As String = ControlChars.CrLf & _
    "<SCRIPT LANGUAGE=""JAVASCRIPT"">" & ControlChars.CrLf & _
    "<!--" & ControlChars.CrLf & _
    "function InsertCurrentDate(VenusDateControl)" & ControlChars.CrLf & _
    "   {" & ControlChars.CrLf & _
    "        var curDate = new Date();" & ControlChars.CrLf & _
    "        var formattedDate = curDate.getMonth() + '/'" & _
    " + curDate.getDate() + '/' + curDate.getFullYear();" _
    & ControlChars.CrLf & _
    "        document.all.item(VenusDateControl).value = formattedDate;" _
    & ControlChars.CrLf & _
    "    }" & ControlChars.CrLf & _
    "// -->" & ControlChars.CrLf & _
    "</SCRIPT>"

#End Region

#Region " Overrides "
     Protected Overrides Sub AddAttributesToRender(ByVal_
                 writer As System.Web.UI.HtmlTextWriter)
        MyBase.AddAttributesToRender(writer)
        writer.AddAttribute("ondblclick", _
           "InsertCurrentDate('" & ClientID & "');")
    End Sub

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

#End Region

#Region " Private Methods "
     Private Sub RegisterVenusDateIncludeScript()
        Dim location As String = Nothing
        If Not Page.IsClientScriptBlockRegistered(DateIncludeScriptKey) Then
            ' Create the client script block.
            Page.RegisterClientScriptBlock(DateIncludeScriptKey,_
                                DateCurrentDateInsertScript)
        End If
    End Sub

#End Region

End Class

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
Venugopal Mallarapu is a .NET geek working as a Software Consultant. He has about 6 years of development experience in Microsoft Technologies. Has started working on .NET from it's early beta release. Is currently spending time designing & architecting enterprise .NET applications.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Michael Bookatz22-Dec-09 4:49
Michael Bookatz22-Dec-09 4:49 
It tells me nothing about how to do it.
GeneralGreat Pin
SerhatAdali28-Aug-05 6:40
SerhatAdali28-Aug-05 6:40 
GeneralErrors in Page Code Pin
Valtur03-Sep-04 4:34
Valtur03-Sep-04 4:34 
GeneralThe page you requested cannot be found. Pin
dzzxyz14-Apr-04 16:51
dzzxyz14-Apr-04 16:51 
GeneralRe: The page you requested cannot be found. Pin
Venugopal Mallarapu15-Apr-04 5:28
Venugopal Mallarapu15-Apr-04 5:28 
GeneralRe: The page you requested cannot be found. Pin
Venugopal Mallarapu15-Apr-04 5:37
Venugopal Mallarapu15-Apr-04 5:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.