网页上的所有控件

网页上的所有控件

本文介绍了循环遍历 asp.net 网页上的所有控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要遍历我的 asp.net 网页中的所有控件并对控件执行一些操作.在一种情况下,我从页面中制作了一个巨大的字符串并将其通过电子邮件发送给我自己,而在另一种情况下,我将所有内容保存到 cookie 中.

I need to loop through all the controls in my asp.net webpage and do something to the control. In one instance I'm making a giant string out of the page and emailing it to myself, and in another case I'm saving everything to a cookie.

问题在于母版页和其中包含控件集合的项目.我希望能够将 Page 传递给该方法,然后让该方法足够通用以遍历最内部内容页面中的所有控件并使用它们.我试过用递归来做这个,但我的递归是不完整的.

The problem is masterpages and items with collections of controls inside them. I want to be able to pass in a Page to the method, then have that method be generic enough to loop through all controls in the inner-most content page and work with them. I've tried doing this with recursion, but my recursion is incomplete.

我想将一个 Page 对象传递给一个方法,并让该方法循环遍历最内层内容页面中的所有控件.我怎样才能做到这一点?

I want to pass a Page object into a method, and have that method loop through all controls in the innermost content page. How can I achieve this?

    private static String controlToString(Control control)
{
    StringBuilder result = new StringBuilder();

    String controlID = String.Empty;

    Type type = null;

    foreach (Control c in control.Controls)
    {
        try
        {
            controlID = c.ID.ToString();

            if (c is IEditableTextControl)
            {
                result.Append(controlID + ": " + ((IEditableTextControl)c).Text);
                result.Append("<br />");
            }
            else if (c is ICheckBoxControl)
            {
                result.Append(controlID + ": " + ((ICheckBoxControl)c).Checked);
                result.Append("<br />");
            }
            else if (c is ListControl)
            {
                result.Append(controlID + ": " + ((ListControl)c).SelectedValue);
                result.Append("<br />");
            }
            else if (c.HasControls())
            {
                result.Append(controlToString(c));
            }

            //result.Append("<br />");
        }
        catch (Exception e)
        {

        }
    }

    return result.ToString();
}

没有尝试/捕捉

未将对象引用设置为对象的实例.

在线控制ID = .....

On line controlID = .....

推荐答案

如果您从文档的根元素开始,您的原始方法将不起作用:例如 page.Controls,因为您只会循环访问第一级控件,但请记住,控件可以是复合的.所以你需要递归来实现它.

Your original method will not work if you start from the root element of your document: something like page.Controls as you will only loop through the first level of controls, but remember a control can be composite. So you need recursion to pull that off.

        public void FindTheControls(List<Control> foundSofar, Control parent)
        {

            foreach(var c in parent.Controls)
            {
                  if(c is IControl) //Or whatever that is you checking for
                  {

                      foundSofar.Add(c);

                      if(c.Controls.Count > 0)
                      {
                            this.FindTheControls(foundSofar, c);
                      }
                  }


            }

        }

这篇关于循环遍历 asp.net 网页上的所有控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 17:53