Dynamically Created User Controls

A

Andrew

I have been reading some of the User Control postings and just want to ask a
simple question to verify whether or not this would be the preferred or
recommended way to implement.

I have an aspx page that has some text boxes, a drop down list and a
placeholder control into which I dynamically load user controls. The User
Control that is loaded into the placeholder is determined by the value in
the drop down box. Thus I potentially need to change the User Control
loaded in the postback event of the aspx page. There are six User Controls
in all.

Woudl the recommended implementation be to load all the controls in the
Page_Init method and load them to the PlaceHolder control in that method.
Then in my Page Load event and the Drop down list postback I would just
hide/show the User Control that I needed? I think that seems to be the
consensus from some of the posts that I have been reading. If that is the
case then I have a couple questions.

-- How is ViewState handled for the controls that are contained on the User
Controls (Drop down List, Data Grid Text Boxes, etc.) Do I need to do
anything manually?
-- When is the Page_Load event for the User Control going to be fired?
Would that happen after the Page Load event for the page?

Not sure if this is enough information for some feedback, but would
appreciate any feedback that anyone would have that has done something
similar that could help me out.

Thanks.
 
E

Ezra Epstein

If you have a finite list of controls then the solution you mention works
quite well. Here's some sample code:

in the .ascx file:
<snip>
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="DeclaritivePortalContent.ascx.cs"
Inherits="Com.Prajna.OnTapV2.CapacityMap.UserControls.DeclaritivePortalConte
nt" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<%@ Register TagPrefix="ot" TagName="UserHome" Src="UserHome.ascx" %>
<%@ Register TagPrefix="ot" TagName="Browse" Src="Browse.ascx" %>
<%-- Etc. place as many as you need here --%>

<ot:browse id="Browse" Visible="False" runat="server"
EnableViewState="True"></ot:browse>
<ot:userhome id="UserHome" Visible="False" runat="server"
EnableViewState="True"></ot:userhome>
<%-- Etc. use the registered controls however you like --%>

</snip>

in the code-behind (c#) you want to cycle through which one to display.
There are some things to remember. One is that if the choice of which UC to
display is the result of a post-back then you won't know about that choice
at Page_Load, so you have to handle the display logic in pre-render. The
code then makes one assumption: that the "name" of the view to display is
it's ID as declared in the page/control where it is being used. I.e., if
the viewName (see below) is "Browse" then the control with an ID="Browse"
will be displayed. Here's the overridden OnPreRender() method

<snip>
protected override void OnPreRender(EventArgs e)
{
string viewName = GetViewName(Context, this.Page); // Assume this
exists (set by your drop-down list...)

// Make the right control visible
if (viewName != null || !IsPostBack)
{
foreach (System.Web.UI.Control c in Controls)
{
if (c.ID == viewName || (viewName == null && c.ID ==
"UserHome")) // make "UserHome" the default view
{
// Here we go, we found the view to enable, 3 methods does
the trick
c.Visible = true;
c.EnableViewState = true;
c.DataBind();
}
else
{
// all other's get disabled
c.Visible = false;
c.EnableViewState = false;
}
}
}
// call the base method
base.OnPreRender (e);
}
</snip>

Hope that helps,

Ezra E.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top