1

ユーザーコントロールをページに動的にロードしています。私は通常vb.netの人で、通常はうまくいくことができますが、これには困惑しています。ロードする前に、変数をページからコントロールに渡そうとしています。

コントロールを呼び出す場所は次のとおりです。

    Control webUserControl = (Control)Page.LoadControl("~/controls/carousel-guards.ascx");

    phGuardsList.Controls.Add(webUserControl);

carousel-guards.ascx に次のプロパティを追加しました。

public String PostCode
        {
            get
            {
                return this.PostCode;
            }
            set
            {
                this.PostCode = value;
            }
        }

しかし、私は webUserControl.PostCode を利用できないようです。

どんな助けでも本当に感謝します

編集 -もちろん、コントロールを参照する必要があります。愚かな私!ただし、carousel_guards で呼び出すことはできません。Error 96 The type or namespace name 'carousel_guards' could not be found (are you missing a using directive or an assembly reference?) C:\Development\Guards247\g247 Test\FindGuard.aspx.cs 197 13 g247 Test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace g247_Test.controls
{
    public partial class carousel_guards : System.Web.UI.UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public String PostCode
        {
            get
            {
                return this.PostCode;
            }
            set
            {
                this.PostCode = value;
            }
        }
    }
}
4

3 に答える 3

1

機能するには、ページのクラス名を使用する必要があります。

var webUserControl = (carousel_guards)Page.LoadControl("~/controls/carousel-guards.ascx");

// now works
webUserControl.PostCode = "17673";
phGuardsList.Controls.Add(webUserControl);

コントロールの参照を含めておらず、クラスの名前が見つからない場合は、aspx 行に挿入できます。

<%@ Reference Control="~/controls/carousel-guards.ascx" %>

または、ページ内にドラッグ アンド ドロップして参照を作成し、実際のコントロールを削除します。動的にするためです。

于 2012-11-13T11:11:19.320 に答える
0

ロードされたコントロールを にキャストしているため、コントロールからプロパティにアクセスできませんControl。コントロールの型にキャストする必要があります。Control クラス名が次のCarouselGuards場合は、次のことができます。

CarouselGuards webUserControl = (CarouselGuards)Page.LoadControl("~/controls/carousel-guards.ascx");

その後、プロパティにアクセスできます。

webUserControl.PostCode = "123";
于 2012-11-13T11:11:26.877 に答える
0

CarouselGuardsの代わりに、コントロール タイプのキャストを使用します。Control

CarouselGuards webUserControl = (CarouselGuards)Page.LoadControl("~/controls/carousel-guards.ascx");
webUserControl.PostCode = "XXXX";

補足として、オブジェクトの null チェック コントロールを忘れないでください。

于 2012-11-13T11:12:16.043 に答える