获取复选框在C#中按自定义属性名称控制
我有一个40-50左右的复选框集合,我为每个复选框设置了一个属性’attr-ID’,这是数据库中唯一的值ID。 如何通过c#代码中的属性名称获取控件。 我想根据页面加载事件的dB值检查一些复选框。
这是你想要的吗?
protected void Page_Load(object sender, EventArgs e) { var cb = FindControlByAttribute(this.Page, "attr-ID", "111"); } public T FindControlByAttribute(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 (c, attributeName, attributeValue); if (cb != null) return cb; } return null; }
if (rdCervical.Attributes["attr-ID"] != null) { string id = rdCervical.Attributes["attr-ID"]; rdCervical.Checked = true; }
我假设您以编程方式添加复选框。 在这种情况下,创建一个容器
并将所有复选框放入其中。 然后只需使用checkContainer.InnerHtml
并使用此库解析该代码。 然后,您可以轻松地使用库API按属性查找元素,我认为该方法是SelectNodes
没有这个库,没有简单的方法可以从代码中导航HTML元素。
- 如何在数据库中为托管的.NET MVC网站存储Sessionstate
- ASP.NET 5 EntityFramework.Core 7.0.0-rc1-final issue – 编译器想要引用7.0.0.0,但未找到
- 在web.config中的appSettings中使用“&”
- 如何在asp.net中获取没有查询字符串值的url?
- 带有UpdatePanel回发的ajax“加载”图标
- 使用Eval在TemplateField中显示毫秒数
- 2 DataTable之间的差异
- Azure中的C#DateTime.Now未报告GMT – 是否可以设置默认时区?
- 如何将图像从flex应用程序传递到asp net c#web服务?