如何加载所有控件并获取属性 [英] How load all controls and get attributes
本文介绍了如何加载所有控件并获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要获取所有控件的属性(ASP控件和HTml控件)我的代码是错误的。
I need get Attributes of all controls (ASP Controls and HTml controls) my code is wrong.
Sub checkControls(controls As ControlCollection)
For Each conrol As Control In controls
Dim ctr As WebControl = DirectCast(conrol, WebControl)
If Not IsNothing(ctr.Attributes("data-action")) Then
Dim dataAction As String = ctr.Attributes("data-ction").Trim.ToLower
If dataAction <> "btn-select" Then
conrol.Visible = False
End If
End If
If conrol.HasControls Then
checkControls(conrol.Controls)
End If
Next
End Sub
推荐答案
因为System.Web.UI.AttributeCollection没有GetEnumerator的公共定义所以你唯一的方法就是使用Keys prop本类的erty以获取所有属性的键
As System.Web.UI.AttributeCollection does not have a public definition for GetEnumerator so the only way that you have is just using Keys property of this class in order to get all attributes' keys
public void CheckControls(ControlCollection controls)
{
foreach (Control control in controls)
{
WebControl ctr = control as WebControl;
if (ctr != null)
{
foreach (string key in ctr.Attributes.Keys)
{
//Do whatever you want
if (key == "sth")
{
string dataAction = ctr.Attributes[key].Trim().ToLower();
control.Visible = false;
}
}
if (control.HasControls() == true)
CheckControls(control.Controls);
}
}
}
这篇关于如何加载所有控件并获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文