Solved

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

Posted on 2010-08-23
11
1,004 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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
 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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