读书人

怎么遍历一个页面下的所有textbox控件

发布时间: 2012-12-21 12:03:49 作者: rapoo

如何遍历一个页面上的所有textbox控件
现在我的页面上有50个textbox:txba,txbb,txb2,txbaay....
我怎么遍历出全部的textbox并设置他为只读?
[最优解释]

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org啊/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form runat="server" id="form1">

<asp:TextBox ID="txt" runat="server" />
<asp:TextBox ID="TextBox1" runat="server" />
<asp:TextBox ID="TextBox2" runat="server" />
<asp:TextBox ID="TextBox3" runat="server" />
<asp:TextBox ID="TextBox4" runat="server" />
<asp:TextBox ID="TextBox5" runat="server" />
<asp:TextBox ID="TextBox6" runat="server" />
<asp:TextBox ID="TextBox7" runat="server" />
</form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control item in form1.Controls)
{
if (item is TextBox)
{
((TextBox)item).ReadOnly = true;
}
}
}
}

[其他解释]
for(int i=0;i<10;i++)
{
TextBox txt=this.FindControls("txt"+i) as TextBox;
if(txt!=null){}
}
[其他解释]
foreach (Control c in this.form1.Controls)
{
if (c is TextBox)
{
((TextBox) c).Enabled = false;

}
}





//这里只考虑一级的,如果有些子控件中有textbox的,要找出来得用递归,得使劲往里刨,往祖坟上刨。
[其他解释]

引用:
foreach (Control c in this.form1.Controls)
{
if (c is TextBox)
{
((TextBox) c).Enabled = false;

……
学习。。。
[其他解释]
引用:
foreach (Control c in this.form1.Controls)
{
if (c is TextBox)
{
((TextBox) c).Enabled = false;

……

+1
[其他解释]
引用:
foreach (Control c in this.form1.Controls)
{
if (c is TextBox)
{
((TextBox) c).Enabled = false;

}
}



//这里只考虑一级的,如果有些子控件中有textbox的,要找出来得用递归,得使劲往里刨,往祖坟上刨。


对的+1
[其他解释]
 foreach (Control item in form1.Controls)
{
if (item is TextBox)
{
((TextBox)item).ReadOnly = true;
}
}

[其他解释]

int k = 0;
protected void Page_Load(object sender, EventArgs e)
{
try
{
k = 0;
txtControl( form1.Controls);

}


catch (Exception ex)
{

throw ex;
}
}

private void txtControl(ControlCollection controls )
{
foreach (Control item in controls)
{
if (item.Controls.Count > 0)
{
txtControl(item.Controls);
}
else
{
if (item is TextBox)
{
((TextBox)item).Text = k.ToString();
k++;
}
}
}

}


[其他解释]
引用:
C# code

int k = 0;
protected void Page_Load(object sender, EventArgs e)
{
try
{
k = 0;
txtControl( form1.Controls);

}
c……

+
[其他解释]
引用:
C# code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/……

+1
[其他解释]
引用:
for(int i=0;i<10;i++)
{
TextBox txt=this.FindControls("txt"+i) as TextBox;
if(txt!=null){}
}
.
[其他解释]
foreach (Control item in form1.Controls)
{
if (item is TextBox)


{
((TextBox)item).ReadOnly = true;
}
}
这个明了
[其他解释]
看控件ID命名有规律不,有就按规律遍历,没有就有点麻烦,像楼上说的,往祖坟上刨
[其他解释]
怎么往祖坟里刨?怎么递归?
[其他解释]
我的TEXTBOX在表单的表格表格里,我使用foreach (Control item in controls) { if (item.Controls.Count > 0) { txtControl(item.Controls); } else { if (item is TextBox) { ((TextBox)item).Text = k.ToString(); k++; } } } 这种方法找不到TEXTBOX,咋办啊?

读书人网 >asp.net

热点推荐