【问题标题】:Iterate though all controls on asp.net page遍历asp.net页面上的所有控件
【发布时间】:2011-06-21 09:08:04
【问题描述】:

我正在使用 ascx,我需要遍历所有控件并选择每个将 cssClass 属性设置为“必需”的控件。

我有以下代码:

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)
                                {

                                }
                            }
                        }
                    }
                }
            }
        }

但是.. 我无法访问 childControl.CssClass。如何访问它?

提前致谢!

【问题讨论】:

    标签: asp.net css iteration


    【解决方案1】:

    CssClass 属性是WebControl class 的成员。

    你必须检查控件是否是一个webcontrol,或者,如果它只是一个控件,你可以在attributes集合中获取属性“class”。

    例如,你可以这样做:

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

    您还必须递归迭代。代码在这里:Using C# to recursively get a collection of controls from a 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);
        }
    }
    

    【讨论】:

    • 我尝试了以下但仍然无法正常工作.. if(childControl is WebControl) { childControl.CssClass }
    • 不要忘记 ChildControls 不是递归的
    • 感谢史蒂夫。现在出现一件事。我无法访问 Web 控件的 Value 属性?这是为什么呢?
    • Value 不是 webcontrol 的成员。你到底想做什么?
    【解决方案2】:

    Control 类没有 CssClass 属性,WebControl 有。因此,请尝试将您的 childControl 转换为 WebControl。如果可行,那么您可以访问 CssClass 属性。

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

    【讨论】:

    • 谢谢汉斯。现在出现一件事。我无法访问 Web 控件的 Value 属性?这是为什么呢?
    • @Johan:同样的问题。 “WebControl”没有 Value 属性,因此您需要将其进一步转换为“真实”类型 - 只有当它确实是那种类型时才会起作用。
    【解决方案3】:

    关于您对上述答案的评论,您需要先检查它是否为WebControl,然后将其转换为WebControl

    var webControl = childControl as WebControl;
    
    if(webControl != null)
    {
       if(webControl.CssClass == 'required')
       // Do your stuff
    }
    

    【讨论】:

    • CodeAnalysis 会抱怨你投了两次(“is”和“as”)。更好(如更快)将只使用“as”,然后检查 null。
    • 不知道!谢谢你的提示。不确定它是否应该得到-1,因为我们正在谈论纳秒,如果那样的话。我的代码可以工作。
    猜你喜欢
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多