Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 循环浏览asp.net网页上的所有控件_C#_Asp.net_Webforms_Master Pages - Fatal编程技术网

C# 循环浏览asp.net网页上的所有控件

C# 循环浏览asp.net网页上的所有控件,c#,asp.net,webforms,master-pages,C#,Asp.net,Webforms,Master Pages,我需要遍历asp.net网页中的所有控件,并对该控件执行一些操作。在一个例子中,我用页面制作了一个巨大的字符串并通过电子邮件发送给自己,在另一个例子中,我将所有内容保存到一个cookie中 问题在于母版页和包含控件集合的项。我希望能够将一个页面传递给该方法,然后使该方法具有足够的通用性,能够循环通过最内部内容页面中的所有控件并使用它们。我试着用递归来做这件事,但我的递归是不完整的 我想将一个页面对象传递给一个方法,并让该方法在最内层内容页面中的所有控件中循环。我怎样才能做到这一点 pri

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

问题在于母版页和包含控件集合的项。我希望能够将一个页面传递给该方法,然后使该方法具有足够的通用性,能够循环通过最内部内容页面中的所有控件并使用它们。我试着用递归来做这件事,但我的递归是不完整的

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

    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();
}
私有静态字符串控制字符串(控制)
{
StringBuilder结果=新建StringBuilder();
String controlID=String.Empty;
Type=null;
foreach(Control.Controls中的控件c)
{
尝试
{
controlID=c.ID.ToString();
如果(c是IEditableTextControl)
{
result.Append(controlID+“:”+((IEditableTextControl)c).Text);
结果。追加(“
”); } else if(c是ICheckBoxControl) { result.Append(controlID+”:“+((ICheckBoxControl)c).选中); 结果。追加(“
”); } else if(c是ListControl) { result.Append(controlID+”:“+((ListControl)c).SelectedValue); 结果。追加(“
”); } else if(c.HasControls()) { 结果:追加(controlToString(c)); } //结果。追加(“
”); } 捕获(例外e) { } } 返回result.ToString(); }
没有尝试/抓住

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


联机controlID=…

如果从文档的根元素开始,则原始方法将不起作用:类似于page.Controls,因为您将仅循环通过第一级控件,但请记住,控件可以是复合的。所以你需要递归来实现这一点

sub getcontrols(byref c as control, byref allControls as list(of control)
if c isnot nothing
allcontrols.add(c)
if c.controls.count>0 then
for each ctrl as control in c.controls
getcontrols(ctrl,allcontrols)
next
end if
        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);
                      }
                  }


            }  

        }
public void查找控件(列表已找到,控件父级)
{
foreach(父控件中的var c)
{
如果(c是IControl)//或者您正在检查的任何东西
{
截至目前,添加(c);
如果(c.Controls.Count>0)
{
这。找到了控制(foundSofar,c);
}
}
}  
}

这应该可以做到。不过,如果有多个占位符,您可能需要进行一些检查,以确保实际处理的是正确的ContentPlaceHolder

我比较喜欢David Finley的linq FindControl方法

公共静态类页面扩展
{
公共静态IEnumerable All(此控件集合控件)
{
foreach(控件中的控件)
{
foreach(Control.Controls.All()中的Control孙辈)
收益回报孙子;
收益率控制;
}
}
}
用法:

// get the first empty textbox
TextBox firstEmpty = accountDetails.Controls
    .All()
    .OfType<TextBox>()
    .Where(tb => tb.Text.Trim().Length == 0)
    .FirstOrDefault();

// and focus it
if (firstEmpty != null)
    firstEmpty.Focus();
//获取第一个空文本框
TextBox firstEmpty=accountDetails.Controls
.All()
第()类
.Where(tb=>tb.Text.Trim().Length==0)
.FirstOrDefault();
//集中注意力
if(firstEmpty!=null)
firstEmpty.Focus();
这对我很有效

只需确保以如下所示的前缀开始标识所有控件。例如:
。否则,解析器将不会检测到您的控件。如果有人在不知道/硬编码控件ID的情况下有更好的解析方法,那就更好了

protected String GetControls(Control control)
{
    //Get text from form elements
    String text = "";
    foreach (Control ctrl in control.Controls)
    {
        if (ctrl.ClientID.Contains("TextBox"))
        {
            TextBox tb = (TextBox)ctrl;
            text += tb.ID.Replace("TextBox", "") + ": " + tb.Text + "<br />";
        }
        if (ctrl.ClientID.Contains("RadioButtonList"))
        {
            RadioButtonList rbl = (RadioButtonList)ctrl;
            text += rbl.ID.Replace("RadioButtonList", "") + ": " + rbl.SelectedItem.Text + "<br />";
        }
        if (ctrl.ClientID.Contains("DropDownList"))
        {
            DropDownList ddl = (DropDownList)ctrl;
            text += ddl.ID.Replace("DropDownList", "") + ": " + ddl.SelectedItem.Text + "<br />";
        }

        if (ctrl.ClientID.Contains("CheckBox"))
        {
            CheckBox cb1 = (CheckBox)ctrl;
            text += cb1.ID.Replace("CheckBox", "") + ": " + cb1.Text + "<br />";
        }

        if (ctrl.HasControls())
            text += GetControls(ctrl);
    }

    return text;
}

请查找下面的代码。这将帮助您完成所需的所有控制。您应该能够使用网页控件以及ASP.NET控件

public partial class Default : System.Web.UI.Page
{

List<Control> lstControl = new List<Control>();

protected void Page_Load(object sender, EventArgs e)
{

}

private List<Label> getLabels() // Add all Lables to a list
{
    List<Label> lLabels = new List<Label>();

    foreach (Control oControl in Page.Controls)
    {
        GetAllControlsInWebPage(oControl);
    }

    foreach (Control oControl in lstControl)
    {
        if (oControl.GetType() == typeof(Label))
        {
            lLabels.Add((Label)oControl);
        }
    }
    return lLabels;
}

protected void GetAllControlsInWebPage(Control oControl)
{
    foreach (Control childControl in oControl.Controls)
    {
        lstControl.Add(childControl); //lstControl - Global variable
        if (childControl.HasControls())
            GetAllControlsInWebPage(childControl);
    }
}

}
public部分类默认值:System.Web.UI.Page
{
List lstControl=新列表();
受保护的无效页面加载(对象发送方、事件参数e)
{
}
私有列表getLabels()//将所有标签添加到列表中
{
List lLabels=新列表();
foreach(第页控件中的控件oControl)
{
GetAllControlsInWeb页面(oControl);
}
foreach(lstControl中的Control-oControl)
{
if(oControl.GetType()==typeof(Label))
{
添加((标签)oControl);
}
}
返回标签;
}
受保护的无效GetAllControlsInWeb页面(控件oControl)
{
foreach(oControl.Controls中的Control-childControl)
{
lstControl.Add(childControl);//lstControl-全局变量
if(childControl.HasControls())
GetAllControlsInWeb页面(childControl);
}
}
}

尽管这个问题已经讨论了9年多,但我在这里留下了基于@Jagadheesh Venkatesan的代码为我工作的代码

private List<Control> getControls()
{
    List<Control> lControls = new List<Control>();
    foreach (Control oControl in Page.Controls)
        foreach (Control childControl1 in oControl.Controls)
            foreach (Control childControl2 in childControl1.Controls)
                foreach (Control childControl3 in childControl2.Controls)
                    if (childControl3.GetType().ToString().Contains("System.Web.UI.WebControls"))
                        lControls.Add(childControl3);
    return lControls;
}
private List getControls()
{
List lControls=新列表();
foreach(第页控件中的控件oControl)
foreach(oControl.Controls中的Control childControl1)
foreach(childControl1.Controls中的Control childControl2)
foreach(在childControl2.Controls中控制childControl3)
if(childControl3.GetType().ToString()包含(“System.Web.UI.WebControls”))
lControls.Add(childControl3);
返回lControl;
}

您得到的错误是什么?没有错误,我的字符串没有代表每个控件的条目。您没有得到错误的原因是您有一个try/catch块将其隐藏。移除它,看看会发生什么。好的,那么我可以将一个页面对象传递给它吗?还有,使用var c比控制c有什么好处?@MAW74656语法糖,如果我看的话,它们是一样的
String log;
foreach (Control ctrl in Page.Controls)
    log += GetControls(ctrl);
public partial class Default : System.Web.UI.Page
{

List<Control> lstControl = new List<Control>();

protected void Page_Load(object sender, EventArgs e)
{

}

private List<Label> getLabels() // Add all Lables to a list
{
    List<Label> lLabels = new List<Label>();

    foreach (Control oControl in Page.Controls)
    {
        GetAllControlsInWebPage(oControl);
    }

    foreach (Control oControl in lstControl)
    {
        if (oControl.GetType() == typeof(Label))
        {
            lLabels.Add((Label)oControl);
        }
    }
    return lLabels;
}

protected void GetAllControlsInWebPage(Control oControl)
{
    foreach (Control childControl in oControl.Controls)
    {
        lstControl.Add(childControl); //lstControl - Global variable
        if (childControl.HasControls())
            GetAllControlsInWebPage(childControl);
    }
}

}
private List<Control> getControls()
{
    List<Control> lControls = new List<Control>();
    foreach (Control oControl in Page.Controls)
        foreach (Control childControl1 in oControl.Controls)
            foreach (Control childControl2 in childControl1.Controls)
                foreach (Control childControl3 in childControl2.Controls)
                    if (childControl3.GetType().ToString().Contains("System.Web.UI.WebControls"))
                        lControls.Add(childControl3);
    return lControls;
}