刚才已经看了很多遍例的例子了...试了一下,没一个管用- -|这么写的...大家帮忙看看问题出哪了目的:做一下提交页面的参数过滤 foreach (Control myControl in Page.Controls[1].Controls)
{
try
{
if (((TextBox)myControl).Text.Contains("-"))
{ Response.Write(@"<script>alert(' 有非法字符串 ');</script>"); }
}
catch { }
}
{
try
{
if (((TextBox)myControl).Text.Contains("-"))
{ Response.Write(@"<script>alert(' 有非法字符串 ');</script>"); }
}
catch { }
}
为什么不报错因为你catch到错误了 并且catch里什么都没做,方法就已经结束了
foreach (Control myControl in Page.Controls[1].Controls)
{
if(myControl is TextBox)
{
TextBox txt = myControl as TextBox;
if (txt.Text.Contains("-"))
{ Response.Write(@" <script>alert(' 有非法字符串 '); </script>");
}
}
不过你要保证Page.Controls[1].Controls这里边有textbox不然也遍历不出来
这是控件深度的问题了
Page.Controls[1].Controls改成this.form1.Controls
你在不包含textbox的容器里边遍历 能出来才有鬼