Solved

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

Posted on 2010-08-23
11
960 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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 …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now