Building Composite Controls with the toolbox

N

None

I'm building my first ASP.NET 2.0 app. I'm familiar with 1.0 and 1.1 and
have read various books, so I'm not a complete beginner. However, I've
become stumped on something that I feel should be easy, but is causing me
some confusion. I've searched on the net, but can't find a resolution.

I'm trying to build a server control that inherits from CompositeControl,
I would like it to be visible in the toolbox.

Here is exactly what I'm doing.

1. Create a new "ASP.NET 2.0 Web Site" (c#)
2. Add another new project (c#\windows\web control library)
3. Open the class "WebCustomControl1.cs"
4. Modify the class (notice I've changed the base blass):

----------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebControlLibrary1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl1 runat=server></
{0}:WebCustomControl1>")]
public class WebCustomControl1 : CompositeControl
{
protected TextBox tb = new TextBox();

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
return tb.Text;
}

set
{
tb.Text = value;
}
}

protected override void CreateChildControls()
{
base.Controls.Add(tb);
base.CreateChildControls();
}

}
}
------------------
5. Compile the solution

6. In the web site project add a reference to the web control library
project (This adds a bin folder to my website with the DLL inside)

7. One problem, there's no item in the toolbox when opening a web page
like default.aspx.

8. If I add a new Web custom control to the web control library project
and recompile, then open default.aspx. the new control
(webcustomcontrol2) is in the toolbox.

9. I can drag it on the form I get the HTML
<cc1:WebCustomControl2 ID="WebCustomControl2_1" runat="server" />
I can rename it to:
<cc1:WebCustomControl1 ID="WebCustomControl2_1" runat="server" />
This displays the text box from my CompositeControl and it works fine.



Why can't I see the composite control in the toolbox?

One strange thing I found, If I right click on the webcontrol project and
select "Remove" then add it in again. Both controls are visible in the
toolbox. However is I exit visual studio and restart it's gone again.

I don't any problems with the second control (based on WebControl) only
the first control (based on CompositeControl)

Do I need more code in the Composite control? I feel I may be missing
something obvious.

Please please help.

Thanks,
Craig
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top