Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compound custom controls, C#

Status
Not open for further replies.

gorgor

Programmer
Aug 15, 2002
164
US
I'm trying to make a ASP.NET custom control in C# (NOT a user control) that contains multiple controls. I'm having a hard time seeing how to create the control so that when dropped into a web application (in Visual Studio), all the sub-controls will be displayed. It seems there are some methods missing in my control class that need to be there to actually display all the controls. Anyone have a simple example of a custom control that will display, say, a couple buttons when dropped into a web application?

This seems to be straight forward when creating a user control, but I want this to be a part of my control library instead of copying an ascx file wherever I want to use the control.

Thanks in advance.
 
Sorry I don't have an example handy, but here is some basic info...

When you are creating this control, you will add a new item of type "Web Custom Control" from the templates in the Add New Item dialog of Visual Studio. This will create a new class that inherits UserControl, along with the Attributes that make the whole drag 'n drop / designer thing work.

The primary method you are going to deal with is the override of the Render() method. Here you will create the controls and have the writer.RenderControl() each of them.

Hope that makes sense.
 
I liked the post so I wrote a simple composite control. I did not inherite from WebControl because I did not use all the properties that come with it.
draggonwell is right in regards to State Management.
protected override void LoadViewState(Object savedState)
protected override object SaveViewState()
protected override void Render(HtmlTextWriter output)

Here is a simple composite control and it is done with a text editor so I do not know how it will behave in VS.NET.
Code:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Cappmgr.CompositeControls 
{
	public class ConcateComposite : Control, INamingContainer
	{
		TextBox s_str1;
		TextBox s_str2;
		Label s_res;

		public void OnConcate(Object sender, EventArgs e)
		{
			s_res.Text = s_str1.Text + s_str2.Text;
			ConcateStrings(this, EventArgs.Empty);
		}

		public void OnClear(Object sender, EventArgs e)
		{
			s_str1.Text = "";
			s_str2.Text = "";
			s_res.Text = "";
		}

		protected void Page_Load(object src, EventArgs e)
		{
			EnsureChildControls();
		}

		public event EventHandler ConcateStrings;

		protected override void CreateChildControls()
		{
			s_str1 = new TextBox();
			s_str2 = new TextBox();
			s_res = new Label();

			Controls.Add(s_str1);
			Controls.Add(new LiteralControl(" concatenated with "));
			Controls.Add(new LiteralControl("<br/>"));
			Controls.Add(s_str2);
			Controls.Add(new LiteralControl(" is "));
			Controls.Add(new LiteralControl("<br/>"));
			Controls.Add(s_res);
			Controls.Add(new LiteralControl("<br/>"));

			Button concate = new Button();
			concate.Text = "Concate";
			concate.Click += new EventHandler(this.OnConcate);
			Controls.Add(concate);

			Button clear = new Button();
			clear.Text = "Clear";
			clear.Click += new EventHandler(this.OnClear);
			Controls.Add(clear);

		}
	}
}

Here is a test stub
Code:
<%@ Page Language="C#" %>
<%@ Register TagPrefix="cc1" Namespace="Cappmgr.CompositeControls" Assembly="ConcateComposite" %>
<script runat="server">

    private void Page_Load(object sender, System.EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lblTestConcateComposite.Text = "Test Concate Composite";
        }
    }
    private void MyCtrl_OnConcateStrings(Object src, EventArgs e)
    {
        lblTestConcateComposite.Text = "Concate Clicked";
    }

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <asp:Label id="lblTestConcateComposite" runat="server"></asp:Label>
        <br />
        <CC1:CONCATECOMPOSITE id="MyCtrl" runat="server"
        OnConcateStrings="MyCtrl_OnConcateStrings"></CC1:CONCATECOMPOSITE>
    </form>
</body>
</html>
Hope this helps,
Marty
btw, I did a preview post and it is to wide not sure how to fix it. Maybe because I cut and pate into this reply box.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top