When does x.Controls.Add(y) work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Lowe

    When does x.Controls.Add(y) work?

    I'm trying to add the content of a user control dynamically at run time,
    dependent upon some request attribute.

    My user control is a simple tell-tall so I'm sure that's not spoiling
    anything.
    <%@ Control Language="C#" AutoEventWireup ="true"
    CodeBehind="cpS tatus.ascx.cs" Inherits="uc.cp Status" %>

    <p>status indicator for special users</p>

    In my master page I have this...
    <%@ Master Language="C#" AutoEventWireup ="true" CodeBehind="pla in.master.cs"
    Inherits="mp.pl ain" %>

    <asp:ContentPla ceHolder ID="FirstSideBa rContainer" runat="server">

    <!-- side bar may be empty -->

    </asp:ContentPlac eHolder>

    and in the master page's code behind I have this...

    protected void Page_Load(objec t sender, EventArgs e)

    {

    if (true == true)

    {

    ContentPlaceHol der ph1;

    uc.cpStatus uc1;

    ph1 = (ContentPlaceHo lder)FindContro l("FirstSideBar Container");

    uc1 = new uc.cpStatus();

    ph1.Controls.Ad d(uc1);

    }

    }


    I can step through and debug and every line gets executed without error.
    When ph1 is initialized its value is null. When it's assigned the value of
    the ContentPlaceHol der contol found by name it has a control count of 1
    (that must be the HTML comment asa text node). After the
    ph1.Controls.Ad d(uc1) statemment it has a control count of 2, so my user
    control would seem to have been added successfully but nothing (other than
    the original comment) appears in my placeholder in my page.

    What am I doing wrong?



  • Cowboy \(Gregory A. Beamer\)

    #2
    Re: When does x.Controls.Add( y) work?

    It looks like you are finding the container correctly, but the norm for
    loading ASCX controls is like so:

    Control uc1= LoadControl("UC 1.ascx");

    The rest of your code should work fine with this. There are some ways around
    doing it this way, but it requires writing a lot of plumbing already in the
    ..NET libraries.

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog


    or just read it:


    *************** *************** **************
    | Think outside the box! |
    *************** *************** **************
    "Brian Lowe" <brian__lowe@ho tmail.comwrote in message
    news:u81InmMHJH A.1088@TK2MSFTN GP02.phx.gbl...
    I'm trying to add the content of a user control dynamically at run time,
    dependent upon some request attribute.
    >
    My user control is a simple tell-tall so I'm sure that's not spoiling
    anything.
    <%@ Control Language="C#" AutoEventWireup ="true"
    CodeBehind="cpS tatus.ascx.cs" Inherits="uc.cp Status" %>
    >
    <p>status indicator for special users</p>
    >
    In my master page I have this...
    <%@ Master Language="C#" AutoEventWireup ="true"
    CodeBehind="pla in.master.cs" Inherits="mp.pl ain" %>
    >
    <asp:ContentPla ceHolder ID="FirstSideBa rContainer" runat="server">
    >
    <!-- side bar may be empty -->
    >
    </asp:ContentPlac eHolder>
    >
    and in the master page's code behind I have this...
    >
    protected void Page_Load(objec t sender, EventArgs e)
    >
    {
    >
    if (true == true)
    >
    {
    >
    ContentPlaceHol der ph1;
    >
    uc.cpStatus uc1;
    >
    ph1 = (ContentPlaceHo lder)FindContro l("FirstSideBar Container");
    >
    uc1 = new uc.cpStatus();
    >
    ph1.Controls.Ad d(uc1);
    >
    }
    >
    }
    >
    >
    I can step through and debug and every line gets executed without error.
    When ph1 is initialized its value is null. When it's assigned the value of
    the ContentPlaceHol der contol found by name it has a control count of 1
    (that must be the HTML comment asa text node). After the
    ph1.Controls.Ad d(uc1) statemment it has a control count of 2, so my user
    control would seem to have been added successfully but nothing (other than
    the original comment) appears in my placeholder in my page.
    >
    What am I doing wrong?
    >
    >
    >

    Comment

    • Brian Lowe

      #3
      Re: When does x.Controls.Add( y) work?

      That does it!

      Thanks!

      "Cowboy (Gregory A. Beamer)" <NoSpamMgbworld @comcast.netNoS pamMwrote in
      message news:OTC4iENHJH A.1308@TK2MSFTN GP02.phx.gbl...
      It looks like you are finding the container correctly, but the norm for
      loading ASCX controls is like so:
      >
      Control uc1= LoadControl("UC 1.ascx");
      >
      The rest of your code should work fine with this. There are some ways
      around doing it this way, but it requires writing a lot of plumbing
      already in the .NET libraries.
      >
      --
      Gregory A. Beamer
      MVP, MCP: +I, SE, SD, DBA
      >
      Subscribe to my blog

      >
      or just read it:

      >
      *************** *************** **************
      | Think outside the box! |
      *************** *************** **************
      "Brian Lowe" <brian__lowe@ho tmail.comwrote in message
      news:u81InmMHJH A.1088@TK2MSFTN GP02.phx.gbl...
      >I'm trying to add the content of a user control dynamically at run time,
      >dependent upon some request attribute.
      >>
      >My user control is a simple tell-tall so I'm sure that's not spoiling
      >anything.
      ><%@ Control Language="C#" AutoEventWireup ="true"
      >CodeBehind="cp Status.ascx.cs" Inherits="uc.cp Status" %>
      >>
      > <p>status indicator for special users</p>
      >>
      >In my master page I have this...
      ><%@ Master Language="C#" AutoEventWireup ="true"
      >CodeBehind="pl ain.master.cs" Inherits="mp.pl ain" %>
      >>
      > <asp:ContentPla ceHolder ID="FirstSideBa rContainer" runat="server">
      >>
      > <!-- side bar may be empty -->
      >>
      > </asp:ContentPlac eHolder>
      >>
      >and in the master page's code behind I have this...
      >>
      >protected void Page_Load(objec t sender, EventArgs e)
      >>
      >{
      >>
      > if (true == true)
      >>
      > {
      >>
      > ContentPlaceHol der ph1;
      >>
      > uc.cpStatus uc1;
      >>
      > ph1 = (ContentPlaceHo lder)FindContro l("FirstSideBar Container");
      >>
      > uc1 = new uc.cpStatus();
      >>
      > ph1.Controls.Ad d(uc1);
      >>
      > }
      >>
      >}
      >>
      >>
      >I can step through and debug and every line gets executed without error.
      >When ph1 is initialized its value is null. When it's assigned the value
      >of the ContentPlaceHol der contol found by name it has a control count of
      >1 (that must be the HTML comment asa text node). After the
      >ph1.Controls.A dd(uc1) statemment it has a control count of 2, so my user
      >control would seem to have been added successfully but nothing (other
      >than the original comment) appears in my placeholder in my page.
      >>
      >What am I doing wrong?
      >>
      >>
      >>
      >

      Comment

      Working...