0

マスター ページで作成されていないユーザー コントロールから親ページのコントロールにアクセスする場合、私がしなければならなかったのは次のことだけでした。

Page sample = this.Page;

そして、親ページとそのコントロールにアクセスできました。しかし、そのページがマスター ページで作成された場合、その同じコードは機能せず、そのコントロールに対して null 例外が発生します。

ここで何を変更する必要がありますか?

4

1 に答える 1

1

あなたのマストコードビハインドで

    public partial class SiteMaster : MasterPage
    {
        public string PropertyInMaster { get; set; }

        protected void Page_Init(object sender, EventArgs e)
        {
            PropertyInMaster = "test";
...

あなたのユーザーコントロールで:

protected void Page_Load(object sender, EventArgs e)
{
    var mst = this.Page.Master as SiteMaster;

    Response.Write(mst.PropertyInMaster);

...

また、マスターの contentplaceholders で findcontrol を実行し、これらのコントロールを実行することもできます。mst.FindControl("ContentPlaceHolder1").FindControl("MyTextBox")...

于 2013-10-05T13:46:09.510 に答える