views:

53

answers:

2

I have a UserControl called UC_Widget, it inheriting from System.Web.UI.UserControl and ITextControl and it also overide the function AddParsedSubObject.when i used it like below,it runs well.

 <uc1:UC_Widget ID="UC_Widget1" runat="server">
hello world
</uc1:UC_Widget>

but,it come out a problem: if i want to use this control to contain another user control, how can i do for this?? many thx!

<uc1:UC_Widget ID="UC_Widget1" runat="server">
hello world
    <uc1:UC_Widget ID="UC_Widget2" runat="server">
     guy
    </uc1:UC_Widget>
</uc1:UC_Widget>

thx Nix,i have solved the problem by the AddParsedSubObject method.

protected override void AddParsedSubObject(object obj)
        {
            if (this.HasControls())
            {
                base.AddParsedSubObject(obj);
            }
            else if (obj is LiteralControl)
            {
                HtmlContent.Append(((LiteralControl)obj).Text);
                this.Text = HtmlContent.ToString();
            }
            else
            {
                string text1 = this.Text;

                UC_eClinicWidget tmp = obj as UC_eClinicWidget;
                if (tmp != null)
                {
                    HtmlContent.Append(GetControlHtml(tmp));
                    this.Text = HtmlContent.ToString();
                }
            }
        }
A: 

This would cause an infinite loop... if UC_Widget contains another UC_Widget, then the inner UC_Widget would also contain a UC_Widget and so on. You'll need to come up with a better design.

Tom Vervoort
I'm not sold on this. Might not be the best design, but I think it is possible.
Nix
A: 

While there is probably a better design, this is still possible.

  1. First evaluate that you can't pull out the piece that repeats. In your example a piece that you could* pullout would be the text. If you can break your control into smaller pieces it will make your overall design less complicated.
  2. Make sure you define a stop condition. As with any recursion you have to make it stop or you will get a Stack Overflow :) .

Counter example to @Tom Vervoort

<asp:UpdatePanel>
    <ContentTemplate>
        <asp:UpdatePanel>
            <ContentTemplate>
                Hi there
            </ContentTemplate>
        </asp:UpdatePanel>
    </ContentTemplate>
</asp:UpdatePanel> 
Nix