これらの2つのシナリオには違いがありますか?
(1)通常の子供からマスターページのプロパティにアクセスする
(2)ネストされたマスターページからマスターページのプロパティにアクセスする
次のようなコンテンツページからマスターページのテキストボックスにアクセスしようとしました。
TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
defaultTextbox.Text = a.Text; // defaultTextBox is a textbox control inside default.aspx
動作しますが、ネストされたマスターページに同じメソッドを適用すると、次のようになります。
TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
myTextBox.Text = a.Text; // myTextBox is a textbox control inside child.master
これは機能しません、私は何かが欠けていますか?私はregularepage_loadハンドラー内で両方のコードを呼び出します...
また、コードビハインドからネストされたマスターページ内にテキストボックスの値を設定できないことに気付きました。間違いなく何かが足りないのですが、それは何ですか?この問題に光を当てるために、ここに例があります:
ネストされたマスターページ:
<%@ Master Language="C#" MasterPageFile="MasterPage.master" AutoEventWireup="false" CodeFile="MasterPage2.master.cs" Inherits="MasterPage2" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:textbox id="tx2" runat="server" text="this is two"></asp:textbox>
<asp:contentplaceholder id="newstuff" runat="server"></asp:contentplaceholder>
</asp:Content>
背後にあるコード:
Response.Wrote(tx2.Text);
私は何も得られません、なぜ私は何を逃したのですか?再帰的な検索コントロールも試したことに注意してください。
String str = ((TextBox)((Content)FindControl("Content2")).FindControl("tx2")).Text;
まだ何もありません