Set Title bar styles in WebPart not WebPartZone

Last post 07-22-2008 10:09 AM by sip99sma. 3 replies.

Sort Posts:

  • Set Title bar styles in WebPart not WebPartZone

    08-24-2004, 9:45 AM
    • Member
      200 point Member
    • lportx
    • Member since 08-24-2004, 9:36 AM
    • Weston, FL
    • Posts 42
    I would like an individual webpart to be able to style its titlebar differently than from the other web parts in its enclosing zone, but it appears that an individual webpart cannot control the output of its title bar.

    By disassembling the System.Web.UI.WebControls.WebPart namespace in Reflector, WebPart is rendered by its enclosing WebPartZone. In the method WebPartZone.RenderBody, the collection of WebParts is looped through and passed to the WebPartChrome object for rendering. I see the stack trace:

    WebPartZone.RenderBody()
    --> WebPartChrome.RenderWebPart(writer, webPart)
    ---->WebPartChrome.RenderTitleBar()
    ---->...then later...
    ---->WebPartChrome.RenderPartContents()

    This illustrates that the web part itself does not have control over the rendering of its titlebar.

    The workaround is to eliminate the title chrome for the web part, then override the render method to build the title bar in the manner seen fit. This, however, will require emulating the output of the RenderTitleBar method by the environment to render the .Net client script calls and other necessary attributes.

    However, on duplicating the titlebar that was generated when the title chrome was visible, I ran into a number of Javascript errors which revealed the depth of the challenge this solution would be, in order to fully code the web part functionality:

    * Minimizing web part caused it to disappear
    * Javascript error thrown when in Catalog mode
    .:: lawrenceport.com ::.
  • Re: Set Title bar styles in WebPart not WebPartZone

    09-07-2004, 7:26 PM
    • Contributor
      4,557 point Contributor
    • mharder
    • Member since 11-22-2002, 12:03 PM
    • Redmond, WA
    • Posts 917
    • AspNetTeam
      Moderator
    This is by design. WebParts are designed to be self-contained modules, and they should be able to be used on many different pages. As such, the page developer should control how the chrome is rendered for a WebPart, not the WebPart itself.
    http://blogs.msdn.com/mharder

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: Set Title bar styles in WebPart not WebPartZone

    09-09-2004, 3:05 PM
    • Member
      200 point Member
    • lportx
    • Member since 08-24-2004, 9:36 AM
    • Weston, FL
    • Posts 42
    If this is indeed by design, then the design causes confusion as to what is configurable by the web part, the web part zone, the part chrome, and as to what takes precedence over what.

    For example, you can set border properties on all of these.

    Also, this is a surprising limitation given the capabilities of the web part: if you can set a web part's background color, why can't you set the background color of the title bar?

    I don't see how giving the web part this overridable control of its title bar would limit a web part's use on many page, or negate its mandate as a self-contained module.

    LP

    .:: lawrenceport.com ::.
  • Re: Set Title bar styles in WebPart not WebPartZone

    07-22-2008, 10:09 AM
    • Member
      4 point Member
    • sip99sma
    • Member since 07-22-2008, 9:44 AM
    • Posts 2

    I was just wondering if any further information had come to light regarding this?

     I have a requirement to have various different coloured webparts that maintain their colour when moved around

     

    EDIT--

    my quick and dirty solution...

    Because the ID of the Title bar is predicatable from the control ID, I dynamically create a style from the WebPart's Page_Init, along the lines of...

     

    Style styleHeader = new Style();

    styleHeader.BackColor = Color.FromName('#123123');

    Page.Header.StyleSheet.CreateStyleRule(styleHeader, this.Page, "#WebPartTitle_gwp" + this.ID);

     

Page 1 of 1 (4 items)