2

子ページにあるWebユーザーコントロールからMasterpage変数にアクセスできますか?私はあなたが追加することにより、子ページでそれらにアクセスすることができます知っている、次の子ページ子ページのコントロールからのマスターページ変数の参照?

+0

は脆いですね。何を試しましたか?メンバー変数?公共財産? – n8wrl

+0

良いアイデアのように聞こえます。正しいマスターを持つページでユーザーコントロールが使用されることをどのように知っていますか? – Khan

答えて

1

ユーザーコントロールは、本質的にコントロール外の任意のページを知らないでなければなりません。より良いアプローチは、ページ自体(マスターページまたは標準)が値を設定および取得するために使用するプロパティおよびイベントをコントロールに公開することです。この単純な例ください:ユーザーコントロールが値を持っている必要があるたび

class PassValueEventArgs : EventArgs 
    { 
     public string Value { get; set; } 
    } 

    public event EventHandler<PassValueEventArgs> RequestingValue; 

    public void ControlDoingWork() 
    { 
     PassValueEventArgs e = new PassValueEventArgs(); 
     if (RequestingValue != null) 
     { 
      RequestingValue(this, e); 
     } 
     string fromHandlingPage = "Received " + e.Value + " from a handling page."; 
    } 

を次に、ユーザーコントロールを含むページだけRequestingValueイベントを処理し、ユーザーコントロールに値を送信することができます。さもなければ、さらに簡単な解決のために、あなたがデータバインドすることもできるユーザーコントロールのパブリックプロパティを公開します。
イベント駆動型アプローチの完全な例を追加:
WebUserControl1EventArgs.cs

public class WebUserControl1EventArgs : EventArgs 
{ 
    public double ValueToSquare { get; set; } 
} 

WebUserControl1.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplicationCS1_net20.WebUserControl1" %> 

Text below will display "Nothing passed from parent page." if the event is unhandled, 
else will display the square of the number passed if handled.<br /><br /> 
<asp:Label runat="server" ID="Label1" Font-Bold="true" Font-Size="Larger" Text="Nothing passed from parent page."></asp:Label> 

WebUserControl1.ascx.cs

public partial class WebUserControl1 : System.Web.UI.UserControl 
{ 
    public event EventHandler<WebUserControl1EventArgs> RequestingNumber; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     ControlDoingWork(); 
    } 

    private void ControlDoingWork() 
    { 
     if (RequestingNumber != null) 
     { 
      WebUserControl1EventArgs e = new WebUserControl1EventArgs(); 
      RequestingNumber(this, e); 
      Label1.Text = (e.ValueToSquare * e.ValueToSquare).ToString(); 
     } 
    } 
} 

WebForm1に.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplicationCS1_net20.WebForm1" %> 

<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <uc1:WebUserControl1 ID="WebUserControl11" runat="server" 
      OnRequestingNumber="WebUserControl11_RequestingNumber" /> 
    </div> 
    </form> 
</body> 
</html> 

WebForm1.aspx.cs

public partial class WebForm1 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void WebUserControl11_RequestingNumber(object sender, WebUserControl1EventArgs e) 
    { 
     e.ValueToSquare = 3.3; 
    } 
} 

WebForm2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplicationCS1_net20.WebForm2" %> 

<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <uc1:WebUserControl1 ID="WebUserControl11" runat="server" /> 
    </div> 
    </form> 
</body> 
</html> 

WebForm2.aspx.cs

public partial class WebForm2 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
} 
+0

これはかなり良いアイデアです。上記の完全な例を提供することができますか(ダミーページがユーザーコントロールに値を渡して)?私は、ユーザーがその技術を使ってページからコントロールに値を渡す方法を知りません – chobo

0

使用ページのマスタープロパティ内にあるWebコントロールからアクセスしようとしたときに動作するようには思えない

<%@ MasterType VirtualPath="~/Master.master" %> 

そのマスターページにアクセスする。その後、FindControlメソッドを使用するか、マスターのパブリックプロパティがある場合はそれを使用できます。背後にあるマスターページのコードの例では:

public Label Title { get { return lblTitle; } } 
+0

ユーザーコントロールを使って「何か」を伝達する良い方法があるはずです。コントロールがこのマスターを持っていないページで使用されるとどうなりますか? – n8wrl

+0

次に、そのIDを変数として使用するか、ページの(または他の親コントロールの)FindControlメソッドを使用して、ページからコントロールに直接アクセスできます。その後、ユーザーコントロールの任意のパブリックプロパティ、メソッド、フィールドを使用できます。 –

関連する問題