web-dev-qa-db-ja.com

ページ上の特定の種類のすべてのWebコントロールを取得する

私は、ページ上のすべてのコントロールを取得し、この関連する質問でそれらのタスクを実行する方法について考えてきました。

プログラムでC#DropDownListを検索する方法

ページをスキャンし、すべてのDropDownListコントロールを取得して、リストで返すことができるコードが必要です。

現在、個々のコントロールをそれぞれ編集する必要があります。むしろ、タスクを実行するために各コントロールを動的にループすることができます。

31
Anicho

my previous SO answer を確認してください。

基本的に、アイデアはコントロールコレクションを反復処理する再帰をラップすることです:

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);
    }
}

そしてそれを使用するには:

List<DropDownList> allControls = new List<DropDownList>();
GetControlList<DropDownList>(Page.Controls, allControls )
foreach (var childControl in allControls )
{
//     call for all controls of the page
}

[2013年11月26日編集]:この目標を達成するためのよりエレガントな方法を次に示します。コントロールツリーを両方向にウォークできる2つの拡張メソッドを作成しました。メソッドは列挙可能なものを生成するため、よりLinqの方法で記述されます。

/// <summary>
/// Provide utilities methods related to <see cref="Control"/> objects
/// </summary>
public static class ControlUtilities
{
    /// <summary>
    /// Find the first ancestor of the selected control in the control tree
    /// </summary>
    /// <typeparam name="TControl">Type of the ancestor to look for</typeparam>
    /// <param name="control">The control to look for its ancestors</param>
    /// <returns>The first ancestor of the specified type, or null if no ancestor is found.</returns>
    public static TControl FindAncestor<TControl>(this Control control) where TControl : Control
    {
        if (control == null) throw new ArgumentNullException("control");

        Control parent = control;
        do
        {
            parent = parent.Parent;
            var candidate = parent as TControl;
            if (candidate != null)
            {
                return candidate;
            }
        } while (parent != null);
        return null;
    }

    /// <summary>
    /// Finds all descendants of a certain type of the specified control.
    /// </summary>
    /// <typeparam name="TControl">The type of descendant controls to look for.</typeparam>
    /// <param name="parent">The parent control where to look into.</param>
    /// <returns>All corresponding descendants</returns>
    public static IEnumerable<TControl> FindDescendants<TControl>(this Control parent) where TControl : Control
    {
        if (parent == null) throw new ArgumentNullException("control");

        if (parent.HasControls())
        {
            foreach (Control childControl in parent.Controls)
            {
                var candidate = childControl as TControl;
                if (candidate != null) yield return candidate;

                foreach (var nextLevel in FindDescendants<TControl>(childControl))
                {
                    yield return nextLevel;
                }
            }
        }
    }
}

thisキーワードのおかげで、これらのメソッドは拡張メソッドであり、コードを簡素化できます。

たとえば、ページ内のすべてのDropDownListを見つけるには、次を呼び出すことができます。

var allDropDowns = this.Page.FindControl<DropDownList>();

yieldキーワードを使用しているため、およびLinqは列挙の実行を延期するのに十分スマートなので、次のように呼び出すことができます(たとえば)。

var allDropDowns = this.Page.FindDescendants<DropDownList>();
var firstDropDownWithCustomClass = allDropDowns.First(
    ddl=>ddl.CssClass == "customclass"
    );

列挙は、Firstメソッドの述語が満たされるとすぐに停止します。コントロールツリー全体を歩くことはできません。

59
Steve B
foreach (DropDownList dr in this.Page.Form.Controls.OfType<DropDownList>())
{

}
15
marko

このまさに質問があり、スティーブBの答えが有用であるとわかったとき、拡張メソッドが必要だったので、リファクタリングしました。

    public static IEnumerable<T> GetControlList<T>(this ControlCollection controlCollection) where T : Control
    {
        foreach (Control control in controlCollection)
        {
            if (control is T)
            {
                yield return (T)control;
            }

            if (control.HasControls())
            {
                foreach (T childControl in control.Controls.GetControlList<T>())
                {
                    yield return childControl;
                }
            }
        }
    }
5
bicbmx

以下は、別の引数を使用する代わりに、要求されたタイプのコントロールコレクションを返す再帰バージョンです。

using System.Collections.Generic;
using System.Web.UI;
// ...
public static List<T> GetControls<T>(ControlCollection Controls)
where T : Control {
  List<T> results = new List<T>();
  foreach (Control c in Controls) {
    if (c is T) results.Add((T)c);
    if (c.HasControls()) results.AddRange(GetControls<T>(c.Controls));
  }
  return results;
}

クラスに挿入します(静的オプション)。

5
Greg Searle

次のように、再帰ロジックを使用してすべてのコントロールを取得できます。

private void PopulateSelectList(Control parentCtrl, List<DropDownList> selectList)
{
    foreach (Control ctrl in parentCtrl.Controls)
    {
        if (ctrl is DropDownList)
        {
            selectList.Add(((DropDownList)ctrl);
            continue;
        }
        FindAllControls(ctrl, selectList);
    }
}
1
James Johnson

ページ上のコントロールをループするのは難しくありません。各コントロール内でさらにコントロールを探す必要があります。

次のようなことができます

foreach(var control in Page)
{
    if(control is DropDownList)
    {
        //Do whatever
    }
    else
    {
        //Call this function again to search for controls within this control
    }
}
1
Mark Williams

System.web.uiのフォームコンポーネントを使用する場合、これは機能しますが、system.web.mvcからフォームコンポーネントを使用する場合は明らかに機能しないため、次の回避策を考え出しました。

for (Int32 idx = 0; idx < formCollection.Count; idx += 1)
                    {
                    String Name = formCollection.Keys[idx];
                    String value = formCollection[idx];

                    if (Name.Substring(0, 3).ToLower() == "chk")

                        {
                        Response.Write(Name + " is a checkbox <br/>");
                        }
                    else if (Name.Substring(0, 5).ToLower() == "txtar")
                        {
                        Response.Write(Name + " is a text area <br/>");
                        }
                    else if (Name.Substring(0, 2).ToLower() == "rd")
                        {
                        Response.Write(Name + " is a RadioButton <br/>");
                        }

                    }

これは私にとってはうまくいきますが、ラジオボタンが選択されていない場合はnullであることがわかったので、何も返さないので、nullの場合はデータベースに何かを書き込む必要はありません

0
        var dropDownLists = new List<DropDownList>();
        foreach (var control in this.Controls)
        {
            if (control is DropDownList)
            {
                dropDownLists.Add( (DropDownList)control );
            }
        }
0
dudeNumber4