1

タイプ(つまり)がある場合、ユーザーコントロール(.ascx)をインスタンス化して使用する方法はありますtypeof(MyUserControl)か?

テキストボックスやドロップダウンリストなどの通常のasp.netコントロールを使用すると、新しいインスタンスを作成して、それをコントロールコレクションに追加できます。これは、ユーザーコントロールでは機能しないようです。新しいインスタンスを作成してコレクションに追加し、そのすべてのイベントを発生させることはできますが、実際にはページにレンダリングされません。通常Page.LoadControl()、.ascxへのパスを使用して呼び出します

あなたが持っているのがそのタイプだけである場合、これは問題を提示します。メソッドに与える.ascxへのパスを取得するにはどうすればよいですかLoadControl。理想的には、Pageオブジェクトへの参照も必要ありません。

4

3 に答える 3

3

いいえ。これは不可能です。マークアップを使用してユーザーコントロールを動的にロードするには、ASCX仮想パスを指定する必要があり、仮想パスのタイプの内部マッピングはありません。

ただし、私はまだ怠惰なので、これは私が使用したアプローチであり、依然として「タイプセーフ」であり、解決する問題をコード内の1つの場所に分離します。それでも「Pageオブジェクト」にアクセスする必要がありますが、それ以外の場合はばかげた詳細を処理します。

簡単な説明は次のとおりです。

  1. タイプを使用して、ヒューリスティックを使用して相対(ルートに対する)ASCXパスを検索し、名前空間から仮想パスにタイプをマップします。手動マッピングを指定する方法を許可し、指定されている場合はそれを使用する
  2. タイプの相対パスを正しい/完全な仮想パスに変換します
  3. コントロールに仮想パスをロードします
  4. 何も起こらなかったかのように続行します

楽しんでください(プロジェクトYMMVから選択したパーツをコピーして貼り付けただけです):

    /// <summary>
    /// Load the control with the given type.
    /// </summary>
    public object LoadControl(Type t, Page page)
    {
        try
        {
            // The definition for the resolver is in the next code bit
            var partialPath = resolver.ResolvePartialPath(t);
            var fullPath = ResolvePartialControlPath(partialPath);
            // Now we have the Control loaded from the Virtual Path. Easy.
            return page.LoadControl(fullPath);
        } catch (Exception ex)
        {
            throw new Exception("Control mapping failed", ex);
        }
    }

    /// <summary>
    /// Loads a control by a particular type.
    /// (Strong-typed wrapper for the previous method).
    /// </summary>
    public T LoadControl<T>(Page page) where T : Control
    {
        try
        {
            return (T)LoadControl(typeof (T), page);
        } catch (Exception ex)
        {
            throw new Exception(string.Format(
                "Failed to load control for type: {0}",
                typeof (T).Name), ex);
        }
    }

    /// <summary>
    /// Given a partial control path, return the full (relative to root) control path.
    /// </summary>
    string ResolvePartialControlPath(string partialPath)
    {
        return string.Format("{0}{1}.ascx",
            ControlPathRoot, partialPath);
    }

ControlResolverのコードリスト:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FooBar
{
    class ControlResolver
    {
        const string BaseNamespace = "TheBaseControlNameSpace";

        readonly IDictionary<Type, string> resolvedPaths = new Dictionary<Type, string>();

        /// <summary>
        /// Maps types to partial paths for controls.
        /// 
        /// This is required for Types which are NOT automatically resolveable
        /// by the simple reflection mapping.
        /// </summary>
        static readonly IDictionary<Type, string> MappedPartialPaths = new Dictionary<Type, string>
        {
            { typeof(MyOddType), "Relative/ControlPath" }, // No virtual ~BASE, no .ASXC
        };

        /// <summary>
        /// Given a type, return the partial path to the ASCX.
        /// 
        /// This path is the path UNDER the Control Template root
        /// WITHOUT the ASCX extension.
        /// 
        /// This is required because there is no mapping maintained between the class
        /// and the code-behind path.
        /// 
        /// Does not return null.
        /// </summary>
        public string ResolvePartialPath (Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            string partialPath;
            if (resolvedPaths.TryGetValue(type, out partialPath))
            {
                return partialPath;
            } else
            {
                string mappedPath;
                if (MappedPartialPaths.TryGetValue(type, out mappedPath))
                {
                    resolvedPaths[type] = mappedPath;
                    return mappedPath;
                } else
                {
                    // Since I use well-mapped virtual directory names to namespaces,
                    // this gets around needing to manually specify all the types above.
                    if (!type.FullName.StartsWith(BaseNamespace))
                    {
                        throw new InvalidOperationException("Invalid control type");
                    } else
                    {
                        var reflectionPath = type.FullName
                            .Replace(BaseNamespace, "")
                            .Replace('.', '/');
                        resolvedPaths[type] = reflectionPath;
                        return reflectionPath;
                    }
                }
            }
        }
    }
}
于 2012-09-30T22:57:46.240 に答える
0
  ' Load the control 
  Dim myUC as UserControl = LoadControl("ucHeading.ascx") 

  ' Set the Usercontrol Type 
  Dim ucType as Type = myUC.GetType() 

  ' Get access to the property 
  Dim ucPageHeadingProperty as PropertyInfo = ucType.GetProperty("PageHeading") 

  ' Set the property 
  ucPageHeadingProperty.SetValue(myUC,"Access a Usercontrol from Code Behind",Nothing) 

  pnlHeading.Controls.Add ( myUC ) 
于 2019-04-23T08:26:45.003 に答える
-1

ここでは運が悪いかもしれません。

この仲間はこれを行うのに助けを得ることができませんでした。

そして彼らはこの人にそれはできないと言いました。

于 2011-03-05T18:11:21.027 に答える