9

あるサイトでは、単一レベルのマスターページのみを使用しており、そのマスターを使用するページでは、 this.Master.FindControl("controlName") を実行してコントロールにアクセスできます。正常に動作します。

ただし、マスターページ レベルが 2 つあるサイトで同じコードを使用すると、MainMaster と、MainMaster をマスターとして持つ SpecificMaster。

そのため、SpecificMaster を使用するページでは、FindControl はオブジェクトに対して null を返しています。私が見ている唯一の違いは、マスターページのネストです。

ブレークポイントを設定して page.Master を見ると、SpecificMaster が表示され、SpecificMaster は MainMaster をマスターとして正しく表示していますが、FindControl はまだ失敗しています。

IE でソースを表示すると、コントロールの名前が正しく表示され、.NET 変更は行われません。

ここで何か考えはありますか?

ティア!

4

7 に答える 7

21

マスター ページをネストしている場合、参照する必要がある追加のコンテナー "コンテンツ" が得られます。

その結果、特定の子ページから FindControl を使用しようとしている場合、通常のアプローチは次のような効果があります。

Label myLabel = (Label)this.Master.FindControl("myLabel");
myLabel.Text = "Success!";

ネストされたマスター ページがあり、子マスターに "myLabel" があるため、このコントロールはコンテンツ コントロール内に含まれます。

したがって、これはコードを次のように変更します。

ContentPlaceHolder ph = (ContentPlaceHolder)this.Master.Master.FindControl("yourContentPane");

Label myLabel = (Label)ph.FindControl("myLabel");
myLabel.Text = "Success!";

そしてVB.NETで

Dim ph As ContentPlaceHolder = DirectCast(Me.Master.Master.FindControl("yourContentPane"), ContentPlaceHolder)

Dim myLabel As Label = DirectCast(ph.FindControl("myLabel"), Label)
myLabel.Text = "Success!"

子ページのコンテンツは最初のマスター ページ コントロールに読み込まれ、その後、祖父母のマスター ページに読み込まれます。

于 2009-10-16T14:36:04.463 に答える
3

試しましたthis.Master.Master.FindControl("controlname");か?

于 2009-10-16T14:01:42.177 に答える
0

私のシナリオは次のとおりでした。この設定が正しいかどうかはわかりませんが、マスターとサブマスターのページ設定が可能になり、制御できるようになりました。

MasterPage -> SubMasterPage -> ASPX ページ

マスターページ:

<asp:ContentPlaceHolder ID="MasterPageContentPlaceHolder" runat="server">
</asp:ContentPlaceHolder>

サブマスターページ:

<asp:Content ID="ModuleMainContent" ContentPlaceHolderID="MasterPageContentPlaceHolder" runat="server">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>

ASPX.cs:

ContentPlaceHolder MainContent = (ContentPlaceHolder)this.Master.Master.FindControl("MasterPageContentPlaceHolder").FindControl("MainContent");
    TextBox var_type = MainContent.FindControl("air") as TextBox;
于 2016-03-09T11:50:53.487 に答える
0

ページ間のポストバックでも同様に機能しています。

ContentPlaceHolder ph = (ContentPlaceHolder)PreviousPage.Master.FindControl("ContentPlaceHolder");

string txt = ((TextBox)(ph.FindControl("UserTextBox"))).Text;

于 2010-04-29T13:50:13.727 に答える
0

私は通常これを行います:</p>

(TextBox)this.Master.FindControl("ContentplaceHolder1").FindControl("TextBox1");
于 2011-07-25T16:24:26.487 に答える
0
HyperLink hl = (HyperLink)Master.Master.FindControl("HyperLink3");

これは、ネストされたマスター ページからコントロールを見つける最も簡単な方法です。

于 2013-02-07T02:50:20.523 に答える