获取CheckBox控件通过自定义在C#属性名称 [英] get checkbox Controls by custom attribute name in C#
本文介绍了获取CheckBox控件通过自定义在C#属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有复选框的集合一些40-50号,我已经为这是在数据库中的唯一值的ID每个复选框中的属性ATTR-ID。我怎样才能获得通过属性名称,在c#code控制。我要检查一些复选框根据在页面加载事件分贝值。
<输入类型=复选框ID =rdCervicalATTR-ID =111RUNAT =服务器/>
解决方案
这是你想要的吗?
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
变种CB = FindControlByAttribute&所述; HtmlInputCheckBox>(this.PageATTR-ID中,111);}
公共ŧFindControlByAttribute< T>(CTL控制,串的attributeName,字符串的AttributeValue)其中T:HTMLControl时
{
的foreach(在ctl.Controls控制C)
{
如果(c.GetType()== typeof运算(T)及及((T)C).Attributes [的attributeName] ==的AttributeValue)
{
返程(T)C;
}
变种CB = FindControlByAttribute< T>(三,的attributeName,的AttributeValue);
如果(CB!= NULL)
返回CB;
}
返回null;
}
I have a collection of checkbox some 40-50 nos and i have set a attribute 'attr-ID' for each checkbox which is a unique value ID in database. how can i get the control by attribute name in c# code. I want to check some of the checkbox according to dB values on page load event.
<input type="checkbox" id="rdCervical" attr-ID='111' runat='server' />
解决方案
Is this what you want?
protected void Page_Load(object sender, EventArgs e)
{
var cb = FindControlByAttribute<HtmlInputCheckBox>(this.Page, "attr-ID", "111");
}
public T FindControlByAttribute<T>(Control ctl, string attributeName, string attributeValue) where T : HtmlControl
{
foreach (Control c in ctl.Controls)
{
if (c.GetType() == typeof(T) && ((T)c).Attributes[attributeName]==attributeValue)
{
return (T) c;
}
var cb= FindControlByAttribute<T>(c, attributeName, attributeValue);
if (cb != null)
return cb;
}
return null;
}
这篇关于获取CheckBox控件通过自定义在C#属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文