简体   繁体   English

遍历asp.net页上的所有控件

[英]Iterate though all controls on asp.net page

I'm using ascx and I need to iterate through all of the controls and selects each that have cssClass attribute set to 'required'. 我正在使用ascx,需要遍历所有控件,并选择每个具有cssClass属性设置为“必需”的控件。

I have the following code: 我有以下代码:

foreach (Control masterControl in Page.Controls)
        {
            if (masterControl is MasterPage)
            {
                foreach (Control formControl in masterControl.Controls)
                {
                    if (formControl is System.Web.UI.HtmlControls.HtmlForm)
                    {
                        foreach (Control contentControl in formControl.Controls)
                        {
                            if (contentControl is ContentPlaceHolder)
                            {
                                foreach (Control childControl in contentControl.Controls)
                                {

                                }
                            }
                        }
                    }
                }
            }
        }

however.. i cannot access childControl.CssClass. 但是..我无法访问childControl.CssClass。 How do I access it? 如何访问?

Thanks in advance! 提前致谢!

CssClass property is a member of the WebControl class . CssClass属性是WebControl类的成员。

You have to check if the control is a webcontrol, or, if it's only a control, you can get the attribute "class" in the attributes collection. 您必须检查控件是否是Web控件,或者,如果仅是控件,则可以在属性集合中获取属性“类”。

for example, you can do : 例如,您可以执行以下操作:

List<WebControl> wcs = new List<WebControl>();
GetControlList<WebControl>(Page.Controls, wcs)
foreach (WebControl childControl in wcs)
{
     if(childControl.CssClass == "required") {
          // process the control
     }
}

You also have to iterate recursively. 您还必须递归迭代。 Code found here : Using C# to recursively get a collection of controls from a controlcollection : 在这里找到的代码: 使用C#从controlcollection递归获取控件的集合

private void GetControlList<T>(ControlCollection controlCollection, List<T> resultCollection)
where T : Control
{
    foreach (Control control in controlCollection)
    {
        //if (control.GetType() == typeof(T))
        if (control is T) // This is cleaner
            resultCollection.Add((T)control);

        if (control.HasControls())
            GetControlList(control.Controls, resultCollection);
    }
}

The Control class doesn't have that CssClass property, the WebControl does. Control类没有CssClass属性,而WebControl有。 So try to cast your childControl to WebControl. 因此,请尝试将childControl为WebControl。 If that worked, then you can access the CssClass property. 如果可行,则可以访问CssClass属性。

WebControl webCtrl = childControl as WebControl;
if (webCtrl != null)
{
   webCtrl.CssClass = "test";
}

Regarding your comment on the answer above, you need to first check that it is a WebControl and then cast it to a WebControl 关于您对以上答案的评论,您需要首先检查它是否为WebControl ,然后将其转换为WebControl

var webControl = childControl as WebControl;

if(webControl != null)
{
   if(webControl.CssClass == 'required')
   // Do your stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM