Home All Groups Group Topic Archive Search About
Author
30 Oct 2006 7:57 PM
Abraham Andres Luna
hello everyone,

this code only removes the first HtmlMeta control even though i loop through
all of the controls:

<%@ Page Language="C#" %>
<script runat="server">
  void Page_Load(Object Sender, EventArgs E)
  {
    foreach (Control ctl in Page.Header.Controls)
      if (ctl.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlMeta")
        Page.Header.Controls.Remove(ctl);
  }
</script>
<html>
<head runat="server">
  <title>HtmlMeta Test Page</title>
  <meta name="description" content="long description" />
  <meta name="keywords" content="keyword list" />
</head>
<body>
  Done!
</body>
</html>

thanks for any help with this.

Author
31 Oct 2006 9:34 AM
marss
Abraham Andres Luna написав:
Show quoteHide quote
> hello everyone,
>
> this code only removes the first HtmlMeta control even though i loop through
> all of the controls:
>
> <%@ Page Language="C#" %>
> <script runat="server">
>   void Page_Load(Object Sender, EventArgs E)
>   {
>     foreach (Control ctl in Page.Header.Controls)
>       if (ctl.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlMeta")
>         Page.Header.Controls.Remove(ctl);
>   }
> </script>
> <html>
> <head runat="server">
>   <title>HtmlMeta Test Page</title>
>   <meta name="description" content="long description" />
>   <meta name="keywords" content="keyword list" />
> </head>
> <body>
>   Done!
> </body>
> </html>
>
> thanks for any help with this.

"The foreach statement is used to iterate through the collection to get
the desired information, but should not be used to change the contents
of the collection to avoid unpredictable side effects..." (MSDN)

for(int i=Page.Header.Controls.Count-1; i>=0; i--)
  if (Page.Header.Controls[i].GetType().ToString() ==
"System.Web.UI.HtmlControls.HtmlMeta")
    Page.Header.Controls.RemoveAt(i);
Author
31 Oct 2006 1:08 PM
Abraham Andres Luna
thanks so much for your reply. i will update my code accordingly.

"marss" <ma***@ukr.net> wrote in message
news:1162287267.707322.71790@m7g2000cwm.googlegroups.com...

Abraham Andres Luna ???????:
Show quoteHide quote
> hello everyone,
>
> this code only removes the first HtmlMeta control even though i loop
> through
> all of the controls:
>
> <%@ Page Language="C#" %>
> <script runat="server">
>   void Page_Load(Object Sender, EventArgs E)
>   {
>     foreach (Control ctl in Page.Header.Controls)
>       if (ctl.GetType().ToString() ==
> "System.Web.UI.HtmlControls.HtmlMeta")
>         Page.Header.Controls.Remove(ctl);
>   }
> </script>
> <html>
> <head runat="server">
>   <title>HtmlMeta Test Page</title>
>   <meta name="description" content="long description" />
>   <meta name="keywords" content="keyword list" />
> </head>
> <body>
>   Done!
> </body>
> </html>
>
> thanks for any help with this.

"The foreach statement is used to iterate through the collection to get
the desired information, but should not be used to change the contents
of the collection to avoid unpredictable side effects..." (MSDN)

for(int i=Page.Header.Controls.Count-1; i>=0; i--)
  if (Page.Header.Controls[i].GetType().ToString() ==
"System.Web.UI.HtmlControls.HtmlMeta")
    Page.Header.Controls.RemoveAt(i);
Author
31 Oct 2006 1:12 PM
Alvin Chooi
You could only remove first HtmlMEta control is because when you are
deleting the first HtmlMeta Control, the ControlCollection in the
Page.Header will be reduced one, which would immediately out of the
foreach. What you need to do is use the for statement, minus one on
looping index whenever you call the Remove() method.


Hope this helps,


Alvin Chooi
Microsoft ASP.NET Enthusiast
http://alvinzc.blogspot.com