Question : Adding new HtmlLink to Me.Page.Header asp.net 4.0 in UserControl

Hi

Trying to add a HtmlLink to a master page from with in a user control.  The below code is a simplify version on the final code missing out the database process. It also needs to be in a user control as it is used across multiple master pages.

The below code works in asp.net 2 but not in asp.net 4 coming up with the error.

'System.Web.HttpException: The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.'

Any help would be great.
1:
2:
3:
4:
5:
6:
7:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim cssLink1 As New HtmlLink()
        cssLink1.Href = "/ExternalFiles/CSS/V5.css"
        cssLink1.Attributes.Add("rel", "stylesheet")
        cssLink1.Attributes.Add("type", "text/css")
        Me.Page.Header.Controls.Add(cssLink1)
    End Sub

Answer : Adding new HtmlLink to Me.Page.Header asp.net 4.0 in UserControl

When I do this in 4.0 it works fine.

Don't you have another issue on your page that creates the error?
1:
2:
3:
4:
5:
6:
7:
8:
private void Page_Load (object sender, EventArgs e)
{
HtmlLink cssLink1 = new HtmlLink();
        cssLink1.Href = "~/css/test.css";
        cssLink1.Attributes.Add("rel", "stylesheet");
        cssLink1.Attributes.Add("type", "text/css");
        Page.Header.Controls.Add(cssLink1);
}
Random Solutions  
 
programming4us programming4us