gpt4 book ai didi

c# - 在 C# 中通过自定义属性名称获取复选框控件

转载 作者:太空狗 更新时间:2023-10-30 00:16:10 26 4
gpt4 key购买 nike

我有大约 40-50 个复选框的集合,我为每个复选框设置了一个属性“attr-ID”,这是数据库中的唯一值 ID。如何在 C# 代码中通过属性名称获取控件。我想根据页面加载事件的 dB 值检查一些复选框。

 <input type="checkbox" id="rdCervical" attr-ID='111' runat='server' />

最佳答案

这是你想要的吗?

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;
}

关于c# - 在 C# 中通过自定义属性名称获取复选框控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8604806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com