Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,
im unable to add meta data keywords , descriptions , etc in page from code behind.
im getting this error :

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).



while the following is the code :
HtmlMeta tag = new HtmlMeta();
tag.Name = "description";
tag.Content = "My description for this page";
Header.Controls.Add(tag);



i have also tried using page.metkeywords , page.header.keyword..
but getting same error
Posted

Assuming your page does not have a masterpage, you can just use it like this:

C#
Header.Keywords = "key1, key2";
      Header.Controls.Add(new HtmlMeta(){Name = "description",Content = "my description"});


and that will actually render:

XML
<head><title>
</title>
<meta name="description" content="my description" /><meta name="keywords" content="key1, key2" />
</head>


you need the header with runat="server" for this to work. if you have a master page you need to do it from there.

but I think your error is about something else.
 
Share this answer
 
Comments
[no name] 1-May-12 2:53am    
My page do have masterpage , i have tried this one also. Bt error still exists
[no name] 1-May-12 13:31pm    
the code I posted was actually done and it was running, so if you can, copy and paste part of your code, it has to be something else, because that works that way. as I said, I think you are doing something else on the page or the header. :)
used <%# instead of <%= in referencing urls in master pages
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900