Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kevon K. Hayes 255 posts 281 karma points
    Aug 10, 2010 @ 16:51
    Kevon K. Hayes
    0

    Attempting to add Meta Element to page Dynamically getting the following Error:

    I have a usercontrol that I'm using to insert the following to a content page:

    <meta name="description" content="Our Lovely Page" />

    public partial class corApplication_MetaDataInjection : System.Web.UI.UserControl
        {
            //Meta Element
            HtmlMeta meta1 = new HtmlMeta();

            //Page Node
            private readonly Node _currentNode = Node.GetCurrent();     
           
            public string metaDescription = "";   


            protected void Page_Load(object sender, EventArgs e)
            {
                metaDescription = _currentNode.GetProperty("corMetaDataDescription").Value.ToString();

                meta1.Name = "description";
                meta1.Content = metaDescription;

                Page.Controls.Add(meta1);
            }
        }

    Server Error in '/' Application.

    The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.


    I've tried Page_Init and override CreateChildeControls events...  Any help would be greatly appreciated.

  • Sascha Wolter 615 posts 1101 karma points
    Aug 10, 2010 @ 18:04
    Sascha Wolter
    0

    Hi Kevon,

    have a look at this:
    http://forums.asp.net/p/1122258/1755180.aspx

    I guess you can't just add the meta tags to the Page.Controls collection but have to use

    Page.Header.Controls.Add(meta1);

    instead.

    Alternatively you could also create a literal control in the header section on the master page where you can write the description to. The above solution is more elegant though. :)

    Hope that helps,
    Sascha

  • Kevon K. Hayes 255 posts 281 karma points
    Aug 10, 2010 @ 22:23
    Kevon K. Hayes
    0

    I'll let you know the results...

  • Kevon K. Hayes 255 posts 281 karma points
    Aug 11, 2010 @ 17:54
    Kevon K. Hayes
    0

    I also have to use the Page_PreInit Event

  • Sascha Wolter 615 posts 1101 karma points
    Aug 11, 2010 @ 18:05
    Sascha Wolter
    0

    Cool, thanks for letting me know!

  • Kevon K. Hayes 255 posts 281 karma points
    Aug 11, 2010 @ 19:02
    Kevon K. Hayes
    0

    Sascha,

    I'm going to have to recant, what I stated above did get rid of the exception thrown however it didn't add the meta element to the page either.  So I'm going to try to use the public property method using your link above.

Please Sign in or register to post replies

Write your reply to:

Draft