Building ASP.NET Custom User Web Control

by marc walgren4. January 2013 01:00

I had an internal project that prompted for a start and end date. I also wanted to "pop-up" a calendar to make the date selection a bit easier. Since there were two date fields to enter and I didn't want to cut and paste code, I created a custom user control. (Thanks to Isaias Formicia-Serna and a CodeProject article from 2004 that got me started). 

 

Clicking the ellipsis bring up the calendar as pictured above. The custom control (ascx) contains all the markup for the controls. The code behind (ascx.cs) contains the page load, property and event handling code. 

A couple key tidbits.

* To access the particular properties of any control (like a text box) inside the custom control, use a public properties (setter or getter) in the custom control's code behind. 

public partial class CtlCalendar : System.Web.UI.UserControl
{
    #region public properties
    public string CalendarDate
    {
        get
        {
            return this.tbCtlCalendarDate.Text;
        }
        set
        {
            this.tbCtlCalendarDate.Text = value;
        }
    }
...

}

* Remember to "Register"  the custom control (ascx) in the page.

 <%@ Register TagPrefix="fbWebReports" TagName="CtlCalendar" Src="~/CtlCalendar.ascx" %>

Using the custom control is easy. Add markup for the custom control just like any standard control.

<fbWebReports:CtlCalendar ID="fbCalStartDate" runat="server" />

 

My source code is available for download.

CtlCalendarUserControl.zip (1.63 kb)

Tags:

.NET | Development

blog comments powered by Disqus

Contact Us  Consulting  Web Development  Data Collection  Flexible Web Lists  Clarion Products  Downloads  How To Order  Site Map  Store  Home
Copyright (c) 1989-2013 Mitten Software Inc., All rights reserved.

Month List