Solved

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

Posted on 2010-08-23
11
988 Views
Last Modified: 2012-05-10
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.
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

Open in new window

0
Comment
Question by:DanielHillier
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 12

Expert Comment

by:UnexplainedWays
ID: 33499536
try it on

protected void Page_PreInit(object sender, EventArgs e)
0
 

Author Comment

by:DanielHillier
ID: 33499660
On

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)

No error message but doesn't load it at all.

0
 
LVL 16

Expert Comment

by:Kamal Khaleefa
ID: 33499681
Protected Overrides Sub OnPreRender(ByVal e As 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 If
    End Sub
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:DanielHillier
ID: 33499692
Thanks for the code.

Protected Overrides Sub OnPreRender(ByVal e As 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

But still get the same error message.

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

Expert Comment

by:Kamal Khaleefa
ID: 33499769
0
 

Author Comment

by:DanielHillier
ID: 33499875
Thanks, but these all seem to be related to versions early then asp.net 4.

The code works find in asp.net 2, 3  but not asp.net 4.

Daniel

0
 
LVL 6

Accepted Solution

by:
ingriT earned 500 total points
ID: 33499966
When I do this in 4.0 it works fine.

Don't you have another issue on your page that creates the error?
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);
}

Open in new window

0
 

Author Comment

by:DanielHillier
ID: 33500028
Your right when I use this code on a blank master with out additional coding the code works.

This code is the only thing that causes the error as the site runs fine when the coded is commented out.

It must a combination of things that cause it to happen.  Like something else happening while the page is loaded stops this code from running correctly.
0
 
LVL 6

Assisted Solution

by:ingriT
ingriT earned 500 total points
ID: 33500055
Can you post your complete code? Or just the onload functions you use? Do you have something else in the Page_Load function?
0
 

Author Comment

by:DanielHillier
ID: 33500138
Umm....

After changing the code below and the positioning the the usercontrol within the header of the master file I found a work around.

If I remove Page.Header. then it has no need to try change the master page and removes the error message.

Thanks for all your help
<%@ Control Language="VB" ClassName="css" %>
<script runat="server">

      
Private Sub Page_Load(sender As Object, e As EventArgs)
        Dim cssLink1 As New HtmlLink()
        cssLink1.Href = "~/css/v5.css"
        cssLink1.Attributes.Add("rel", "stylesheet")
        cssLink1.Attributes.Add("type", "text/css")
        Controls.Add(cssLink1)
    End Sub

</script>

Open in new window

0
 

Author Closing Comment

by:DanielHillier
ID: 33504855
Doesn't really answer the question but helped solve it.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question