首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

请问批量给空间置属性的有关问题

2012-01-30 
请教批量给空间置属性的问题!页面上有很多个同一类型的服务器控件,比如是radiobutton我想在后台写一个代码

请教批量给空间置属性的问题!
页面上有很多个同一类型的服务器控件,比如是radiobutton

我想在后台写一个代码,批量的把所有的控件的属性设置为 radiobutton.enabled = false;

请问有什么简单的办法?

[解决办法]

C# code
foreach (Control c in Page.Controls)            {                if (c.GetType().ToString() == "System.Web.UI.WebControls.RadioButton")                    ((RadioButton)c).Enabled = false;            }
[解决办法]
2楼的应该没错,不过代码写法有点不好,
C# code
//if (c.GetType().ToString() == "System.Web.UI.WebControls.RadioButton")//改为if (c.GetType() == typeof(RadioButton))
[解决办法]
C# code
//page只是一个节点.你的Radiobutton可能是在page.form节点下.所有在不确定节点是哪个的情况下最好循环遍历一下.public void SetRadioButton(Control obj){    for(int i=0;i<obj.Controls.Count;i++)       {            if(obj.Controls[i].Count>0)                 SetRadioButton(obj.Controls[i];            else            {                 if(obj.Controls[i].GetType==typeof(RadioButton))                     ((RadioButton)obj.Controls[i]).Enabled = false;            }       }}protected void Page_Load(object sender, EventArgs e){   SetRadioButton(Page);}
[解决办法]
protected void Page_Load(object sender, EventArgs e)
{
SetRadioButton(this.Page);
}

public void SetRadioButton(Control obj)
{
for (int i = 0; i < obj.Controls.Count; i++)
{
if (obj.Controls[i].Controls.Count > 0)
SetRadioButton(obj.Controls[i]);
else
{
if (obj.Controls[i].GetType() == typeof(RadioButton))
((RadioButton)obj.Controls[i]).Enabled = false;
}
}
}

热点排行