# Friday, January 19, 2007

This is part two in a series of posts about ASP.Net Controls.

ASP.Net Controls, Part 2: Composite Custom Controls - some assembly required

Now these controls are able to be designed to have some reuse and life outside one particular project.  The up side is that you can piece together odds and ends from System.Web.Ui.WebControls just how you like them, and as per normal the actual rendering to HTML is left to ASP.Net.

The down side is you just start with a class, descend from WebControl and piece it together in code.

Here's a bare bones sample to illustrate the concept, but first some notes:

  • Import System.ComponentModel for the attributes on the properties.  This is how they display in your Visual Studio properties window
  • Use actual controls as backing variables for the properties.  You want to be holding instances of the controls you will display in your class, and then abstract their properties behind your own properties.
  • CreateChildControls is where you assemble what will eventually be rendered down to the browser by adding to the Controls collection exposed by the base class.
  • Call EnsureChildControls.  A lot.  :-) 
  • This sample control no behaviour.  Use += / AddHandler to wire up your controls to event handlers.  Omitted for clarity.
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace MyProject.Controls
{

    public class CompositeControl : System.Web.UI.WebControls.WebControl
    {

        TextBox txtInput = new TextBox();
        Button cmdSubmit = new Button();
       
        protected override void CreateChildControls()
        {
            EnsureChildControls();

            txtInput.Text = DefaultText;
            Controls.Add(txtInput);

            cmdSubmit.Text = "Submit";
            Controls.Add(cmdSubmit);

            base.CreateChildControls();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
        }

        [Category("Appearance"), DefaultValue("Enter text here")]
        public string DefaultText
        {
            get
            {
                EnsureChildControls();
                return txtInput.Text;
            }
            set
            {
                EnsureChildControls();
                txtInput.Text = value;
            }
        }

    }
}

Friday, January 19, 2007 2:55:26 PM (AUS Eastern Daylight Time, UTC+11:00)  #    Disclaimer  |  Comments [0]  | 

It occurred to me that ASP.Net has been a topic missing from my blog, so to rectify the situation I'm posting three pieces on Controls in the ASP.Net space.  Took me ages to get this straight in my own head so with any luck it will make sense here.

Part 1:  Web User Controls (ASCX files), the low hanging fruit

What better place to start.  These are the odd one out in the ASP.Net control space for a couple of reasons. 

  • While it is possible, they don't do reuse between projects well. 
  • They do have a drag and drop design time experience!
  • You drag them onto a page from the Solution Explorer, not the toolbox.

First thing to note is they have their own life cycle after Page_Load, and it feels a little like Server.Execute()

While they do have their own code behind class that inherits from System.Web.UI.UserControl, I try to not use too many properties on the class because I feel it gives a misleading impression in implementation.  Just say the thing has a text() property and in my Page_Load I set the text to something, well after Page_Load the thing can do what ever it wants to and then I have to go looking for what happened to my value for the text() property.

Re-use between projects requires some planning because they are not seperate from your web project, but re-use inside the same project is what they are best at.  Things like a navigational device, a customer lookup widget or something that makes an appearance more than once are good examples.  Pull them out, glue them together with a Web User Control, drop them back in.  Easy. 

Friday, January 19, 2007 2:00:54 PM (AUS Eastern Daylight Time, UTC+11:00)  #    Disclaimer  |  Comments [0]  |