For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET > July 2007 > usercontrol inside usercontrol









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author usercontrol inside usercontrol
Spartaco

2007-07-31, 8:10 am

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?


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com