如何用FOREACH遍历页面上所有的TEXTBOX,判断他们是否为空
类别: ASP教程
1.整个页面的判断
foreach(Control ctl in this.Controls[1].Controls)
{
if(ctl.GetType().Name=="TextBox")
{
TextBox tb =new TextBox();
tb=(TextBox)this.FindControl(ctl.ID);
if(tb.Text==string.Empty)
{
Response.Write("<script>alert(\'" + ctl.ID + "的值为空。\');</script>");
break;
}
}
}
2.指定formID里TextBox 判断
先找出你的Form的ID
protected HtmlForm yourformID;
foreach (object obj in yourformID.Controls)
{
if (obj is TextBox)
{
TextBox tb = (TextBox)obj;
if (tb.Text = string.Empty)
{
Response.Write("<script>alert(\'" + tb.ID + "的值为空。\');</script>;")
}
}
}
foreach(Control ctl in this.Controls[1].Controls)
{
if(ctl.GetType().Name=="TextBox")
{
TextBox tb =new TextBox();
tb=(TextBox)this.FindControl(ctl.ID);
if(tb.Text==string.Empty)
{
Response.Write("<script>alert(\'" + ctl.ID + "的值为空。\');</script>");
break;
}
}
}
2.指定formID里TextBox 判断
先找出你的Form的ID
protected HtmlForm yourformID;
foreach (object obj in yourformID.Controls)
{
if (obj is TextBox)
{
TextBox tb = (TextBox)obj;
if (tb.Text = string.Empty)
{
Response.Write("<script>alert(\'" + tb.ID + "的值为空。\');</script>;")
}
}
}
- 上一篇: 制做行背景颜色交替变换的表格
- 下一篇: 实例讲解ASP抓取网上房产的信息
-= 资 源 教 程 =-
文 章 搜 索