ControlCollection.Remove bug

  • Thread starter Abraham Andres Luna
  • Start date
A

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.
 
M

marss

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.

"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.GetType().ToString() ==
"System.Web.UI.HtmlControls.HtmlMeta")
Page.Header.Controls.RemoveAt(i);
 
A

Abraham Andres Luna

thanks so much for your reply. i will update my code accordingly.


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.

"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.GetType().ToString() ==
"System.Web.UI.HtmlControls.HtmlMeta")
Page.Header.Controls.RemoveAt(i);
 
A

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top