从子页面中的控件引用母版页变量?

时间:2012-03-08 18:44:54

标签: asp.net user-controls webforms master-pages web-user-controls

是否可以从子页面中的Web用户控件访问Masterpage变量?我知道您可以通过添加以下内容在子页面中访问它们

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

尝试从子页面内的Web控件进行访问时似乎不起作用

2 个答案:

答案 0 :(得分: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)
    {

    }
}

答案 1 :(得分:0)

使用Page的Master属性访问其主页。之后,您可以使用FindControl方法或使用master的公共属性(如果有)。例如,在后面的母版页代码中:

public Label Title { get { return lblTitle; } }