HELP: (ASP.NET 2.0) - Can't get CSS HtmlLink Element working in VS Designer

Discussion in 'ASP .Net Building Controls' started by Axel Dahmen, Dec 16, 2006.

  1. Axel Dahmen

    Axel Dahmen Guest


    I've got the following problem: In my client's project I've created an ASP.NET Custom Control to add CSS style sheet HtmlLink elements to a page dynamically. But I can't get this control to work in VS Designer. Thus my co-workers can't create their pages as expected.

    !! Please note that I don't have write access to newsgroups at my client so I'm trying to be as precise as possible. I can't reply to questions during the week. !!

    This is my set-up:

    My control (MyStyleLink) resides in the <header> section of the master page:

    <%@ Master Language="C#" ... %>
    <html ...>
    <uc1:MyStyleLink runat="server" />

    That's what's working:

    The control renders a bunch of <link type="text/css" rel="stylesheet" href="...."> elements in the master page's <header> section using the Render() method. It creates a CSS link constructed from the master page file path, a second link constructed from the page file path and additional links for each User Control (.ascx) in the page, constructed from each control's file path.

    Here's what's NOT working (corresponding questions follow below):

    I've created a Designer class derived from ControlDesigner, assigned it to the MyStyleLink class, implemented a GetDesignTimeHtml() method and tried to have it create the same link elements the control creates at runtime. My issues:

    a) At design time, none of the document paths is available,
    neither Page.MasterPageFile
    nor Request.Url
    or TemplateControl.AppRelativeVirtualPath or Page.AppRelativeVirtualPath.
    They all are null (which I noticed debugging my devenv process).

    b) If I hard-code a <link> into the master page, it works in Designer as well.
    If I now use this exact <link> as return value to GetDesignTimeHtml(), like:

    public override string GetDesignTimeHtml()
    return "<link .... />";

    it doesn't.

    c) I tried a third approach, using the control's OnInit() event to add
    HtmlLinks to the master page. Yet I get an HttpException telling me that
    I can't add controls to the Controls collection on Init, Load, DataBinding
    etc. ("HttpException: The control collection cannot be modified during
    DataBind, Init, Load, PreRender or Unload phases.")

    My Questions:

    a) Why are all the paths null?
    b) Why can't I get my control to render the <link> element in Designer?
    c) Why can't I add controls to the master pages Header section?

    Here's my final (and most important) question:

    How can I get my control to work at all ???

    Axel Dahmen
    Axel Dahmen, Dec 16, 2006
    1. Advertisements

  2. Hi Axel,

    Based on my understanding, you're creating a custom server control that is
    designed to insert dynamic stylesheet links into the master page's header
    according to different content page and user controls used on the WebForm.
    This is working correctly at runtime, but you found it's not working at

    Please correct me if I've misunderstood anything.

    I've done some research and consulting with my colleagues, I'm afraid this
    is not possible to change the stylesheet of the master page at runtime
    using custom server control.

    As you've already discovered, to support design-time behavior, we need to
    provide a control designer and overrides either GetDesignTimeHtml() or
    GetEmptyDesignTimeHtml(), GetErrorDesignTimeHtml(). However, html returned
    from these methods are merely used to show a preview image to the user
    instead of directly injecting the html into the web page. Since <link>
    elements are used to change the dynamic behavior of a html page instead of
    has a own UI to represent, it's not possible for a control designer to add
    links into the host web form.

    As for the relevant properties don't have value at design-time, it's
    because most of the services need HttpContext to function properly, and
    it's not available at design-time.

    The exception in OnInit() you're seeing is because adding controls at these
    places will make fail to load viewstate later since viewstate is
    restored based on control hierarchy and position.

    In summary, I'm afraid it's not possible to change the stylesheet links at
    design-time using a custom server control.

    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    Get notification to my posts through email? Please refer to
    ications. If you are using Outlook Express, please make sure you clear the
    check box "Tools/Options/Read: Get 300 headers at a time" to see your reply

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Dec 18, 2006
    1. Advertisements

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. craig dicker
    Peter Rilling
    Jul 10, 2005
  2. WindAndWaves

    css element within element....

    WindAndWaves, Feb 2, 2005, in forum: HTML
    Leif K-Brooks
    Feb 3, 2005
  3. Steve B.
    Cheryl D Wise
    Jun 1, 2007
  4. Jim Richards

    Why can't I can get help on ASP.NET?

    Jim Richards, Dec 25, 2007, in forum: ASP .Net
    Jim Richards
    Dec 29, 2007
  5. HANM
    Joseph Kesselman
    Jan 29, 2008
  6. Cal Who
    Cal Who
    Apr 26, 2010
  7. Alexander Higgins
    Teemu Keiski
    Jul 30, 2007
  8. JeffQ888
    May 23, 2006