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


Axel Dahmen


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

Walter Wang [MSFT]

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 ([email protected], 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.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Latest member

Latest Threads