Dynamically Created User Controls

Discussion in 'ASP .Net Web Controls' started by Andrew, Sep 4, 2003.

  1. Andrew

    Andrew Guest

    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.
     
    Andrew, Sep 4, 2003
    #1
    1. Advertising

  2. Andrew

    Ezra Epstein Guest

    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.

    "Andrew" <karcheraw@NoSpam_hotmail.com> wrote in message
    news:...
    > 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.
    >
    >
     
    Ezra Epstein, Sep 13, 2003
    #2
    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. Bob Rock
    Replies:
    1
    Views:
    687
    Cowboy \(Gregory A. Beamer\)
    Sep 17, 2006
  2. Kees de Winter
    Replies:
    2
    Views:
    522
    Kees de Winter
    Jan 29, 2007
  3. msimmons
    Replies:
    0
    Views:
    544
    msimmons
    Jul 16, 2009
  4. Denise

    Dynamically created user controls

    Denise, Mar 16, 2005, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    134
    Brock Allen
    Mar 16, 2005
  5. John Sewell

    Dynamically Created User Controls not Initialising

    John Sewell, Sep 3, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    139
    Jeffrey Palermo [MCP]
    Sep 5, 2004
Loading...

Share This Page