2

子ページにある Web ユーザー コントロールから Masterpage 変数にアクセスすることは可能ですか? 以下を追加することで、子ページでそれらにアクセスできることを知っています

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

子ページ内にある Web コントロールからアクセスしようとすると動作しないようです

4

2 に答える 2

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)
    {

    }
}
于 2012-03-08T19:31:01.353 に答える
0

Page の Master プロパティを使用して、その masterpage にアクセスします。その後、FindControl メソッドを使用するか、マスターのパブリック プロパティがあればそれを使用できます。たとえば、マスター ページのコード ビハインドでは次のようになります。

public Label Title { get { return lblTitle; } }
于 2012-03-08T18:47:38.530 に答える