Solved

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

Posted on 2010-08-23
11
998 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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