how to access parent container page HtmlGenericControl

Last post 07-18-2008 10:33 AM by Joseph Baggett. 6 replies.

Sort Posts:

  • how to access parent container page HtmlGenericControl

    01-25-2004, 6:15 AM
    • Loading...
    • Haim_w
    • Joined on 03-09-2003, 11:36 AM
    • Israel
    • Posts 54
    Is it possible to gain access within Server control to one of the parent container page HtmlGenericControl?
    I’m looking for something equivalent to this.Parent.FindControl(“BodyTag”).Attributes.Add("bgcolor","black");
    So I can set the Body.bgcolor attribute of my container page.
  • Re: how to access parent container page HtmlGenericControl

    01-26-2004, 1:27 PM
    • Loading...
    • tinghaoy
    • Joined on 06-11-2002, 2:29 PM
    • Beijing, China
    • Posts 228
    • AspNetTeam
    If your Html elements are indeed server side controls (with runat=server), you can always walk up the control hierarchy to find them.

    For example:

    <body runat=server id="BodyTag">
    <form ruant=server>
    ....
    </form>
    </body>

    void Page_Load() {
    BodyTag.Attributes.Add("bgcolor", "black");
    }



    HTH,
    Ting-hao

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: how to access parent container page HtmlGenericControl

    01-27-2004, 8:58 AM
    • Loading...
    • Haim_w
    • Joined on 03-09-2003, 11:36 AM
    • Israel
    • Posts 54
    I will try to sharp my question,
    I developed a web control and I want to set the color of my container page, depending on one of the web control properties. I guess there are 2 options:

    1. The easy and ugly way writing JavaScript Block from the server side :
    <script language=’javaScript’ For=’window’ Event=’onload’>
    document.bgColor = ‘black’;
    </script>

    2. the nice way is to set the container page Body tag to runat=server and to set the BodyTag.Attributes.Add("bgcolor","black");
    as you suggested.

    My question refer to option number 2. will trying to access the BodyTag from the PreRender function in my control I’m having difficulties to find it in the Page hierarchy

    My Code:


    System.Web.UI.HtmlControls.HtmlGenericControl BodyTag = this.Parent.FindControl(“BodyTag”)


    the problem ,as you can see FindControl return System.Web.Ui.Control
    but I need System.Web.UI.HtmlControl to be return.
  • Re: how to access parent container page HtmlGenericControl

    01-27-2004, 11:47 AM
    • Loading...
    • BSC_ZaP!
    • Joined on 01-17-2004, 12:05 PM
    • Memphis, TN
    • Posts 35
    FindControl() works by beginning at the control it was called upon and searching down. You would have to walk up the control tree: (C#)

    Control c = this.Parent;
    while(c.ID != "BodyTag") {
    c = c.Parent;
    }
    HtmlGenericControl body = (HtmlGenericControl)c;
    BSC

  • Re: how to access parent container page HtmlGenericControl

    01-27-2004, 1:21 PM
    • Loading...
    • tinghaoy
    • Joined on 06-11-2002, 2:29 PM
    • Beijing, China
    • Posts 228
    • AspNetTeam
    Yes, you will have to cast your return type.


    System.Web.UI.HtmlControls.HtmlGenericControl BodyTag = (System.Web.UI.HtmlControls.HtmlGenericControl)Page.FindControl(“BodyTag”);


    HTH,
    Ting-hao
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: how to access parent container page HtmlGenericControl

    01-28-2004, 2:25 AM
    • Loading...
    • Haim_w
    • Joined on 03-09-2003, 11:36 AM
    • Israel
    • Posts 54
    Thanks for the help,
    BSC example really worked
  • Re: how to access parent container page HtmlGenericControl

    07-18-2008, 10:33 AM

    if your body tag is runat="server" then it will be inside of a HtmlContainerControl control in the Page.Controls.  You can then gain access to your HtmlForm element within that control inside the container.  If the body tag is not runat="server" the body tag will be inside of an HtmlGenericControl in the Page.Controls.

    Joseph Baggett
    MCSD, MCPD: Web Developer, MCITP: Database Developer, MCTS: SQL Server 2005, MCTS: .NET 2.0 Web Applications, MCTS: .NET Framework 2.0 Windows Applications, MCTS: .NET Framework 2.0 Windows Applications, MCAD, MCP

    If the project doesn't work, blame Scott Guthrie and/or refer to the problem solving flowsheet. O.~
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter