请问在.cs文件中如何查找同一种类型的所有控件? RT,例如我页面上有10个BUTTON,10个TEXTBOX,10个LABEL,我现在就要找出这10个BUTTON来(不知道ID的,最好是个集合,我还要循环,做判断),如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 foreach (Control c in this.Page.Controls) { if(c is Button) { } } foreach (Control c in this.Controls){if(c is Button){}}容器中button,递归查询 -_-#不行啊,他只会循环一次就跳出来了,里面的那IF都没进,并且我确定页面上有Button控件 foreach (Control c in this.Controls)this改为你Button控件所在容器的ID 必须递归调用: protected void Page_Load(object sender, EventArgs e) { FindButton(this.Page); } private void FindButton(Control c) { for (int i = 0; i < c.Controls.Count; i++) { if (c.Controls[i] is Button) Response.Write((c.Controls[i] as Button).ID); else FindButton(c.Controls[i]); } } 这个没试过,不行的话,再执行一句:FindButton(this.Master);试试。 关键就是在你控件所属命名空间里找Master里的就在Master里找,gridview里的就在gridview里找 因为那只是访问了Page的第一层控件。假设页面上有1000个控件,它只是访问了10个。 (TextBox)this.Page.Master.FindControl(masterPageContentPlaceHolderID).FindControl(""); asp.net 验证控件在输入正确信息后,错误提示仍然存在 请大家帮我解决下瓶颈问题,跪谢大家了!! 求助SqlDataReader对象 只能读取一行,无法循环读取!?? 与web页面不在同一目录的图片不能在此web页面中显示。 调试出现如下报错!----在线等!!~ datagrid 的一些问题,奇怪.................. 急!急!请高人进来看看这个保存网页时的奇怪错误?? 请高手指点下 急死我了!!请问如何注销一个自定义的控件? 请问如何让DropDownList的下拉列表宽度显示完整? 查看dll的代码用什么工具? treeview控件使用
{
if(c is Button)
{
}
}
{
if(c is Button)
{}
}
容器中button,递归查询
this改为你Button控件所在容器的ID
protected void Page_Load(object sender, EventArgs e)
{
FindButton(this.Page);
}
private void FindButton(Control c)
{
for (int i = 0; i < c.Controls.Count; i++)
{
if (c.Controls[i] is Button)
Response.Write((c.Controls[i] as Button).ID);
else
FindButton(c.Controls[i]);
}
}
Master里的就在Master里找,gridview里的就在gridview里找
因为那只是访问了Page的第一层控件。假设页面上有1000个控件,它只是访问了10个。