?
Solved

Composite control with design-time support

Posted on 2008-01-29
4
Medium Priority
?
752 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

0
Comment
Question by:Tom Knowlton
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
DropZone earned 1600 total points
ID: 20772891
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
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 20773527
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...
0
 
LVL 18

Expert Comment

by:DropZone
ID: 20773595
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.
0
 

Assisted Solution

by:nomadsolo
nomadsolo earned 400 total points
ID: 20780434
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses
Course of the Month6 days, 14 hours left to enroll

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question