Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Dynamically Created User Controls

Reply
Thread Tools

Dynamically Created User Controls

 
 
Andrew
Guest
Posts: n/a
 
      09-04-2003
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.


 
Reply With Quote
 
 
 
 
Ezra Epstein
Guest
Posts: n/a
 
      09-13-2003
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.UserContr ols.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.

"Andrew" <karcheraw@NoSpam_hotmail.com> wrote in message
news:(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
Cannot access dynamically created user controls Kees de Winter ASP .Net 2 01-29-2007 03:20 PM
Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created) dickster ASP .Net Building Controls 0 12-08-2005 09:32 AM
Dynamically created user controls Denise ASP .Net Building Controls 4 03-16-2005 06:55 PM
Dynamically Created User Controls not Initialising John Sewell ASP .Net Web Controls 1 09-05-2004 01:59 PM



Advertisments