usercontrol inside usercontrol

Discussion in 'ASP .Net' started by Spartaco, Jul 31, 2007.

  1. Spartaco

    Spartaco Guest

    My purpose is whenever I want to make a new UserControl, to have a property
    in the userControl's base class to decide if a frame around the usercontrol
    should be visible or not.

    Since I want the ability to personalize the frame, I have created a
    UserControl with a PlaceHolder where other controls should be inserted:

    <asp:panel id="panel" runat="server" CssClass="frame">
    <table id="title" class="titleFrame" runat="server" cellpadding="0"
    cellspacing="0">
    <tr><td class="first"><h3 id="lblTitle" runat="server"></h3></td>
    <td>
    <asp:Image id="img" runat="server" AlternateText="Zoom" />
    </td>
    </tr>
    </table>
    <asp:placeHolder ID="ph1" runat="server"></asp:placeHolder>
    </asp:panel>

    Now I want a base class for UserControls, with a property to set the frame
    visible, this class will also load my frame and will move the child controls
    to the placeholder contained in the frame:

    public class BaseUserControl : UserControl
    {
    #region fields
    bool _frameVisible;
    string _frameTitle;
    PlaceHolder ph1;
    List<Control> ctls = new List<Control>();
    #endregion

    #region constructor
    public BaseUserControl()
    {
    }
    #endregion

    #region properties

    public bool FrameVisible
    {
    get{return _frameVisible;}
    set{_frameVisible = value;}
    }

    public string FrameTitle
    {
    get{return _frameTitle;}
    set{_frameTitle = value;}
    }
    #endregion

    #region overrides
    protected override void AddParsedSubObject(object obj)
    {ctls.Add((Control)obj);}

    protected override void CreateChildControls()
    {
    if (_frameVisible) {
    Control frame = Page.LoadControl("~/Frame.ascx");
    frame.ID = "frame";
    ph1 = (PlaceHolder)frame.FindControl("ph1");
    HtmlGenericControl title =
    (HtmlGenericControl)frame.FindControl("lblTitle");
    title.InnerHtml = _frameTitle;
    this.Controls.Add(frame);
    }

    base.CreateChildControls();

    foreach (Control ctl in ctls) {
    if (_frameVisible) {
    ph1.Controls.Add(ctl);
    } else {
    Controls.Add(ctl);
    }
    }
    }
    #endregion
    }

    This approch seems to work, but there is a problem with DropDownLists
    viestate, any other controls seems to work correctly after a postback, but
    DropDownLists no. I suspect because these controls uses the interface
    IPostBackDataHandler and my approch breaks something, but I don't understand
    what...

    any help?
     
    Spartaco, Jul 31, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajesh Tiwari

    Can we use a usercontrol inside a usercontrol

    Rajesh Tiwari, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    424
    Rajesh Tiwari
    Aug 12, 2003
  2. nail
    Replies:
    0
    Views:
    1,550
  3. Fabiano
    Replies:
    2
    Views:
    609
    =?Utf-8?B?c3Jpbmk=?=
    Jun 3, 2005
  4. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    189
  5. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    191
    vatech1993
    Dec 11, 2004
Loading...

Share This Page