怎样取到一个aspx页面的所有的类型为button的id foreach(Control ctl in this.Controls){ if(ctl is Button) { Response.Write(ctl.ID+"<br>"); }} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把你的控件都加在一个panel里然后private void Button1_Click(object sender, System.EventArgs e) { string IDCollection=""; foreach(Control c in this.Panel1.Controls) { if( c is Button) IDCollection+=c.ID; } this.Label1.Text=IDCollection; } 我试了一下,老是不会走进判断里面去,也就是不走Response.Write(ctl.ID+"<br>");判断进不去,我哪里写的不合适哦,怎么没有正确地结果哦 代码在什么窗体下?确定this范围 刚才的方法有局限这个就可以了:private void Button1_Click(object sender, System.EventArgs e) { ControlCollection c=this.Controls; GetControlsID(c); this.Label1.Text=IDCollection; } public void GetControlsID( ControlCollection c) { foreach (Control s in c) { if(s is Button) IDCollection+=s.ID; else GetControlsID(s.Controls); } } 在一个页面里 里面的控件的组成是个树型结构 所以要用递归来求光是this.controls是不行的 这个ControlCollection里只有一个Form foreach (Control c in this.Page.Controls[1].Controls) { if(c is Button) ((Button)c).Text=""; } 字符串分割的问题!在线急等 关于文件读写的问题~~! 有关.net控制flash动态路径的问题 为什么模板列会被歧视呢? WPF 中,继承了一个控件后,如何能够动态继承他的外观 【惊人发现】ADO.NET的重大缺陷 asp.net里如何引用OCX控件呢? 在.net中,dll文件如何读取配置文件。 [来者有分]通过编程创建项目? 怎样将两个dataset合并成一个 仅有的10分,希望大家帮助一下 请大侠帮忙纠错, 关于DataSet+dataAdapter插入更新问题 在线=.......谢谢
然后
private void Button1_Click(object sender, System.EventArgs e)
{
string IDCollection="";
foreach(Control c in this.Panel1.Controls)
{
if( c is Button)
IDCollection+=c.ID;
}
this.Label1.Text=IDCollection;
}
Response.Write(ctl.ID+"<br>");
判断进不去,我哪里写的不合适哦,怎么没有正确地结果哦
确定this范围
这个就可以了:
private void Button1_Click(object sender, System.EventArgs e)
{
ControlCollection c=this.Controls;
GetControlsID(c);
this.Label1.Text=IDCollection;
}
public void GetControlsID( ControlCollection c)
{
foreach (Control s in c)
{
if(s is Button)
IDCollection+=s.ID;
else
GetControlsID(s.Controls);
} }
光是this.controls是不行的 这个ControlCollection里只有一个Form
{
if(c is Button)
((Button)c).Text="";
}