如何用foreach遍歷頁面上所有的TextBox
發(fā)表時間:2024-02-04 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]1.整個頁面的判斷 foreach(Control ctl in this.Controls[1].Controls){ if(ctl.GetType().Name=="TextBox") { TextBox tb =new TextBox(); tb=(TextBox)t...
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>;")
}
}
}