troubleshooting Question

Composite control inside a composite control

Avatar of troycomp
troycomp asked on
.NET ProgrammingC#ASP.NET
10 Comments1 Solution651 ViewsLast Modified:
Maybe i'm attacking the problem all wrong but i'm working on a project where on a few pages the user can add a control (outer control) as many times as they wish. BUT inside of this control they can add a row of data (inner control) under that control as much as they want. Meaning outer control 1 could have 5 innner controls, outer control 2 could have 3 inner controls etc.. I tried user controls but they are a real pain. Gridview has to be bound to a data source and there is no data source. Im using composite controls and i can add the outer controls fine but when i add inner controls to the outer controls, it only shows the inner controls of the clicked outer control. If i add an inner control to another outer control, the inner controls disappear of all the other outer controls. But if i add an inner control to the outer control, all the inner controls return for the outer control. Its not losing the controls just not showing them ll at once. Gurus please help.
//default.aspx
 
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="addBtn" runat="server" Text="Add" onclick="Button1_Click" /><br />
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>
 
//default.aspx.cs
 
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
public partial class _Default : System.Web.UI.Page 
{
    private int outerCount = 1;
 
    private bool addBtnClicked = false;
 
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Control myControl = GetPostBackControl(this.Page);
 
        if ((myControl != null))
        {
            if ((myControl.ClientID.ToString() == "addBtn"))
            {
                addBtnClicked = true;
            }
        }
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["outerCount"] = 1;
        }
        if (addBtnClicked)
        {
            AddOneToCount();
        }
        else
        {
            bool ok = Int32.TryParse(ViewState["outerCount"].ToString(), out outerCount);
            if (!ok)
            {
            }
        }
 
        for (int i = 0; i < outerCount; i++)
        {
            OuterControl oc = new OuterControl();
            PlaceHolder1.Controls.Add(oc);
            LiteralControl literalBreak = new LiteralControl("<br />");
            PlaceHolder1.Controls.Add(literalBreak);
        }
 
    }
    public void AddOneToCount()
    {
        bool ok = Int32.TryParse(ViewState["outerCount"].ToString(), out outerCount);
        if (ok)
        {
            outerCount++;
            ViewState["outerCount"] = outerCount;
        }
 
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
 
    }
 
    public static Control GetPostBackControl(Page thePage)
    {
        Control myControl = null;
        string ctrlName = thePage.Request.Params.Get("__EVENTTARGET");
        if (((ctrlName != null) && (ctrlName != string.Empty)))
        {
            myControl = thePage.FindControl(ctrlName);
        }
        else
        {
            foreach (string Item in thePage.Request.Form)
            {
                Control c = thePage.FindControl(Item);
                if (((c) is System.Web.UI.WebControls.Button))
                {
                    myControl = c;
                }
            }
 
        }
        return myControl;
    }
 
}
 
//outer control
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
/// <summary>
/// Summary description for CompositeControl
/// </summary>
public class OuterControl : WebControl, INamingContainer
{
    private int rowCounter = 1;
    Panel pnlAddRow = new Panel();
 
    public OuterControl()
	{
        pnlAddRow.Attributes.Add("runat", "server");
        pnlAddRow.ID = "pnl";
 
        RowCounter = 1;
 
        AddRows();
 
    }
 
    public int RowCounter
    {
        get
        {
            EnsureChildControls();
            object o = ViewState["rowCount"];
            if (o == null)
            {
                return -1;
            }
 
            rowCounter = (int)o;
            return rowCounter;
        }
        set
        {
            EnsureChildControls();
            ViewState["rowCount"] = value;
        }
    }
 
    private void AddRows()
    {
        for (int i = 0; i < rowCounter; i++)
        {
            InnerControl ic = new InnerControl();
            pnlAddRow.Controls.Add(ic);
        }
    }
 
    protected override void CreateChildControls()
    {
        this.Controls.Add(new LiteralControl(@"<table border=""4"">"));
        this.Controls.Add(new LiteralControl(@"<tr>"));
        this.Controls.Add(new LiteralControl(@"<td>I'm the outer control. Click the button to add an inner control --></td>"));
        this.Controls.Add(new LiteralControl(@"<td>"));
        Button addButton = new Button();
        addButton.Attributes.Add("runat", "server");
        addButton.ID = "btn1";
        addButton.Text = "Add";
        addButton.Click += new EventHandler(this.addButton_Click);
        this.Controls.Add(addButton);
 
        this.Controls.Add(new LiteralControl(@"</td></tr>"));
        this.Controls.Add(new LiteralControl(@"<tr><td>"));
        this.Controls.Add(pnlAddRow);
        this.Controls.Add(new LiteralControl(@"</td></tr></table>"));
 
        base.CreateChildControls();
    }
 
    private void addButton_Click(Object sender, EventArgs e)
    {
        RowCounter++;
        AddRows();
    }
 
}
 
//inner control
 
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
/// <summary>
/// Summary description for InnerControl
/// </summary>
public class InnerControl : WebControl, INamingContainer
{
	public InnerControl()
	{
		//
		// TODO: Add constructor logic here
		//
	}
 
    protected override void CreateChildControls()
    {
        this.Controls.Add(new LiteralControl(@"<table>"));
        this.Controls.Add(new LiteralControl(@"<tr>"));
        this.Controls.Add(new LiteralControl(@"<td>I'm the inner control!!</td>"));
        this.Controls.Add(new LiteralControl(@"</tr></table>"));
 
        base.CreateChildControls();
    }
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros