Asp.net 使用linq获取网页中特定类型的web控件列表

Asp.net 使用linq获取网页中特定类型的web控件列表,asp.net,linq,recursion,web-controls,Asp.net,Linq,Recursion,Web Controls,是否有一种方法可以使用linq获取网页中的文本框列表,而不管它们在树层次结构或容器中的位置如何。因此,与其循环遍历每个容器的ControlCollection来查找文本框,不如在linq中执行相同的操作,可能是在单个linq语句中?您将需要递归来遍历所有控件的所有子级。除非出于某种原因,您必须使用LINQ实现这一点(我想您是指lambdas),否则您可以尝试替代它。我见过的一种技术是在ControlCollection上创建一个扩展方法,该方法返回IEnumerable。。。大概是这样的: pu

是否有一种方法可以使用linq获取网页中的文本框列表,而不管它们在树层次结构或容器中的位置如何。因此,与其循环遍历每个容器的ControlCollection来查找文本框,不如在linq中执行相同的操作,可能是在单个linq语句中?

您将需要递归来遍历所有控件的所有子级。除非出于某种原因,您必须使用LINQ实现这一点(我想您是指lambdas),否则您可以尝试替代它。

我见过的一种技术是在ControlCollection上创建一个扩展方法,该方法返回IEnumerable。。。大概是这样的:

public static IEnumerable<Control> FindAll(this ControlCollection collection)
{
    foreach (Control item in collection)
    {
        yield return item;

        if (item.HasControls())
        {
            foreach (var subItem in item.Controls.FindAll())
            {
                yield return subItem;
            }
        }
    }
}
var textboxes = this.Controls.FindAll().OfType<TextBox>();
public static IEnumerable<T> FindAll<T>(this ControlCollection collection) where T: Control
{
    return collection.FindAll().OfType<T>();
}
var textboxes = this.Controls.FindAll<TextBox>().Where(t=>t.Visible);
公共静态IEnumerable FindAll(此ControlCollection)
{
foreach(集合中的控制项)
{
收益回报项目;
if(item.HasControls())
{
foreach(item.Controls.FindAll()中的var子项)
{
收益回报子项;
}
}
}
}
它处理递归。然后您可以在页面上使用它,如下所示:

public static IEnumerable<Control> FindAll(this ControlCollection collection)
{
    foreach (Control item in collection)
    {
        yield return item;

        if (item.HasControls())
        {
            foreach (var subItem in item.Controls.FindAll())
            {
                yield return subItem;
            }
        }
    }
}
var textboxes = this.Controls.FindAll().OfType<TextBox>();
public static IEnumerable<T> FindAll<T>(this ControlCollection collection) where T: Control
{
    return collection.FindAll().OfType<T>();
}
var textboxes = this.Controls.FindAll<TextBox>().Where(t=>t.Visible);
var textboxs=this.Controls.FindAll().OfType();
这将为您提供页面上的所有文本框。您可以更进一步,构建处理类型筛选的扩展方法的通用版本。可能是这样的:

public static IEnumerable<Control> FindAll(this ControlCollection collection)
{
    foreach (Control item in collection)
    {
        yield return item;

        if (item.HasControls())
        {
            foreach (var subItem in item.Controls.FindAll())
            {
                yield return subItem;
            }
        }
    }
}
var textboxes = this.Controls.FindAll().OfType<TextBox>();
public static IEnumerable<T> FindAll<T>(this ControlCollection collection) where T: Control
{
    return collection.FindAll().OfType<T>();
}
var textboxes = this.Controls.FindAll<TextBox>().Where(t=>t.Visible);
公共静态IEnumerable FindAll(此ControlCollection集合),其中T:Control
{
返回集合。FindAll()。of type();
}
你可以这样使用它:

public static IEnumerable<Control> FindAll(this ControlCollection collection)
{
    foreach (Control item in collection)
    {
        yield return item;

        if (item.HasControls())
        {
            foreach (var subItem in item.Controls.FindAll())
            {
                yield return subItem;
            }
        }
    }
}
var textboxes = this.Controls.FindAll().OfType<TextBox>();
public static IEnumerable<T> FindAll<T>(this ControlCollection collection) where T: Control
{
    return collection.FindAll().OfType<T>();
}
var textboxes = this.Controls.FindAll<TextBox>().Where(t=>t.Visible);
var textboxs=this.Controls.FindAll()。其中(t=>t.Visible);
提供了我找到的关于这个问题的最佳答案。我选择了LINQ版本:

/// <summary>
/// Use a LINQ query to find the first focused text box on a windows form.
/// </summary>
public TextBox TextBoxFocusedFirst1()
{
    var res = from box in this.Controls.OfType<TextBox>()
          where box.Focused == true
          select box;
    return res.First();
}
//
///使用LINQ查询查找windows窗体上的第一个焦点文本框。
/// 
公共文本框TextBoxFocusedFirst1()
{
var res=this.Controls.OfType()中的from框
其中box.Focused==true
选择框;
返回res.First();
}

如果您的页面有母版页,并且您知道内容占位符名称,那么这非常简单。我做了一些类似的事情,但是使用了web面板

private void SetPanelVis(string PanelName)
{
    Control topcontent = Form.FindControl("MainContent");           
    foreach (Control item in topcontent.Controls.OfType<Panel>())   
    {
        item.Visible = (item.ID == RadioButtonList1.SelectedValue); 
    }
}
private void SetPanelVis(字符串PanelName)
{
Control topcontent=Form.FindControl(“MainContent”);
foreach(topcontent.Controls.OfType()中的控件项)
{
item.Visible=(item.ID==RadioButtonList1.SelectedValue);
}
}

此解决方案不会递归搜索控件树。搜索是肤浅的。