Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-08-23
11
Medium Priority
?
1,006 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1500 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 1500 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

721 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