We help IT Professionals succeed at work.

Composite control with design-time support

Tom Knowlton
Tom Knowlton asked
on
Medium Priority
784 Views
Last Modified: 2011-04-14
I think I may have reached a point in my development needs where I need greater control, flexibility and power than an .ascx can provide.

But I need a good guide to help me make the transition.  What I need are some good resources for getting my head around custom composite controls with design-time support.

I want to group a bunch of controls and html elements together as a single unit plus have design-time support, such that I can position those elements within the custom control itself.

How can I do this?  I welcome websites and book recommendations and any advice you can give.
//MY COMPLAINT IS THAT THE TEXT BOXES IN THIS SAMPLE (gotton off of MSDN) DO NOT SHOW-UP WHEN I DROP THE CONTROL ONTO A WEB PAGE.  I WANT THEM TO BE VISIBLE AT DESIGN-TIME AND WHEN I CLICK ON THEM I WANT TO SEE THEIR PROPERTIES IN THE PROPERTIES WINDOW.
 
 
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebControlLibrary1 
{
      
      public class Composite : Control, INamingContainer 
      {
            private int number = 100;
            private Label label;
            
            public int Number
            {
                  get
                  {
                        return number;
                  }
                  set
                  {
                        number = value;
                  }
            }
            
            private int Sum
            {
                  get 
                  {
                        EnsureChildControls();
                        return Int32.Parse(((TextBox)Controls[1]).Text) + 
                              Int32.Parse(((TextBox)Controls[4]).Text);
                  }
                  
            }
 
            public string Text
            {
                  get
                  {
                    EnsureChildControls();
                        return label.Text;
                  }
                  set
                  {
                    EnsureChildControls();
                        label.Text = value;
                  }
            }
            
            
            public event CheckEventHandler Check;
            
            protected virtual void OnCheck(CheckEventArgs ce)
            {
                  if (Check != null)
                  {
                        Check(this,ce);
                  }
            }
            
            protected override void CreateChildControls() 
            {
                  
                  Controls.Add(new LiteralControl("<h3>Enter a number : "));
                  
                  TextBox box1 = new TextBox();
                  box1.Text = "0";
                  Controls.Add(box1);
                  
                  Controls.Add(new LiteralControl("</h3>"));
                  
                  Controls.Add(new LiteralControl("<h3>Enter another number : "));
                  
                  TextBox box2 = new TextBox();
                  box2.Text = "0";
                  Controls.Add(box2);
                  
                  Controls.Add(new LiteralControl("</h3>"));
                  
                  Button button1 = new Button();
                  button1.Text = "Submit";
                  Controls.Add(new LiteralControl("<br>"));
                  Controls.Add(button1);
                  button1.Click += new EventHandler(this.ButtonClicked);
                  
                  Controls.Add(new LiteralControl("<br><br>"));
                  label = new Label();
                  label.Height = 50;
                  label.Width = 500;
                  label.Text = "Click the button to see if you won.";
                  Controls.Add(label);
                  
            }
            
            protected override void OnPreRender(EventArgs e)
            {
                  ((TextBox)Controls[1]).Text = "0";
                  ((TextBox)Controls[4]).Text = "0";
            }
            
            private void ButtonClicked(Object sender, EventArgs e)
            {
                  OnCheck(new CheckEventArgs(Sum - Number));
            }
      }
}

Open in new window

Comment
Watch Question

Commented:
In order for a custom composite control to have support in the visual designer,  you need to create a "designer" for it.  In essence, this is a control that instantiates an object of your custom class, in its default state, when the visual designer displays it.

Here are a few sites to help you get started:
http://www.theserverside.net/developmentor/thread.tss?thread_id=25025
http://msdn2.microsoft.com/en-us/library/12yydcke.aspx
Tom KnowltonWeb developer

Author

Commented:
This link is to a white paper, but I don't seem to have rights to view it:
http://www.theserverside.net/developmentor/thread.tss?thread_id=25025

I am making my way through the walkthrough given in the other link -- thank you...

Commented:
Sorry about that.  I guess they pulled the PDF file (I got an error on viewing it too now).

Here is another link for you:
http://msdn2.microsoft.com/en-us/library/aa478960.aspx

The following book is *extremely* good, but it's out of print.  It also relates to .NET 1.1, but the theory of doing things is still the same in .NET 2.0.  I believe there is a newer edition for 2.0, but I'm not sure.

http://www.microsoft.com/MSPress/books/5728.aspx
    -dZ.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.