Search code examples
c#asp.netperformancemaster-pagesfavicon

The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>)


Q:

I want to add favicon.ico to my web application .so i add the icon to my solution then, i write the following:

Login page :

 <link rel="shortcut icon" href="<%=ResolveUrl("~/favicon.ico")%>"/>

every thing is okay.

Master page of the other pages:

<link rel="shortcut icon" href="<%=ResolveUrl("~/favicon.ico")%>"/>

shows the following error :

The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>).

If i use <%# instead and :

protected override void OnLoad (EventArgs e)
{
  base.OnLoad (e);
  Page.Header.DataBind ();
}

Is this less performance ?and how to fix this problem?


Solution

  • You dont need to use any form of databinding, simply put runat="server" and the runtime will pares the tag as a GenericHtmlControl and the Url attribute will be resolved in the same manner as a normal ASP.Net ServerControl.

    <link rel="shortcut icon" runat="server" href="~/favicon.ico" />
    
    // will render as
    <link rel="shortcut icon" href="favicon.ico" />