0

xaml にラベルとテキスト ボックスの負荷を定義するフィールドセットがあります。でも。再帰を使用してテキスト ボックスにアクセスできません。

ここに私のxamlのフィールドセットからのスニペットがあります:

<fieldset runat="server" id="fdst1" class="default_form">
                    <dl>
                        <dt>
                            <asp:Label ID="lblCustomerId" Text="Customer ID" runat="server" />
                        </dt>
                        <dd>
                            <asp:TextBox AutoPostBack="true" OnTextChanged="TextBox_OnTextChanged" ID="txtCustomerId"
                                Enabled="false" runat="server" />
                        </dd>
                    </dl>
                    <dl>
                        <dt>
                            <asp:Label ID="Label1" Text="Associated Brand" runat="server" />
                        </dt>
                        <dd>
                            <asp:TextBox AutoPostBack="true" OnTextChanged="TextBox_OnTextChanged" ID="txtBrandName"
                                Enabled="false" runat="server" />
                        </dd>
                    </dl>

テキストボックスコントロールを取得するために使用するコードは次のとおりです...

private List<DropDownList> GetDropDownLists()
        {
            List<DropDownList> controls = new List<DropDownList>();

            FindControls<DropDownList>(this.Controls, controls);

            return controls;
        }

/// <summary>
        /// this will find all the contols in a collection of a given type.
        /// </summary>
        /// <typeparam name="T">Type of control to find</typeparam>
        /// <param name="Controls">Contol Collection to look through</param>
        /// <param name="foundControls">List of found controls</param>
        public static void FindControls<T>(ControlCollection Controls, List<T> foundControls) where T : class
        {
            T found = default(T);

            if (Controls != null && Controls.Count > 0)
            {
                for (int i = 0; i < Controls.Count; i++)
                {
                    if (Controls[i] is T)
                    {
                        found = Controls[i] as T;
                        foundControls.Add(found);
                        break;
                    }
                    else
                        // Recursive method call.
                        FindControls<T>(Controls[i].Controls, foundControls);
                }
            }
        }

返されるリストは空ですが、ページを渡しています。コントロールが含まれるコントロール コレクション。テーブルを使用する場合、これはすべて正常に機能しますが、上司はそれがフィールドセットにあると主張します。私の質問は、フィールドセットを使用するときに再帰を使用してこれらのテキスト ボックスを取得する方法です。

4

1 に答える 1

1

xaml にラベルとテキスト ボックスの負荷を定義するフィールドセットがあります。

これは XAML ではありません。それはこの質問の目的には関係ありませんが、検索作業を妨げる可能性があります. これは、ASP.Net サーバー ページまたはコントロールのマークアップ部分です。

List<DropDownList> controls = new List<DropDownList>();
FindControls<DropDownList>(this.Controls, controls);

使用しているコードは、特定の種類のコントロールを再帰的に検索します。コードはドロップダウン リストのみを探しています。このようなものがうまくいくと思います:

List<TextBox> controls = new List<TextBox>();
FindControls<TextBox>(this.Controls, controls);

テーブルを使用する場合、これはすべて正常に機能しますが、上司はそれがフィールドセットにあると主張します。私の質問は、フィールドセットを使用するときに再帰を使用してこれらのテキスト ボックスを取得する方法です。

Afieldsetは、テーブルよりもはるかに優れたフォーム要素のコンテナーです。ASP.Net はデフォルトでコントロールを再帰的にトラバースしないため、カスタム再帰が必要です。フォーム要素を新しいコンテナ (つまりfieldset) 内に配置すると、階層にレベルが追加されます。

于 2012-09-17T15:06:37.043 に答える