是否可以从子页面中的 Web 用户控件访问 Masterpage 变量?我知道您可以通过添加以下内容在子页面中访问它们
<%@ MasterType VirtualPath="~/Master.master" %>
尝试从子页面内的 Web 控件访问时,它似乎不起作用
是否可以从子页面中的 Web 用户控件访问 Masterpage 变量?我知道您可以通过添加以下内容在子页面中访问它们
<%@ MasterType VirtualPath="~/Master.master" %>
尝试从子页面内的 Web 控件访问时,它似乎不起作用
用户控件本质上应该不知道控件之外的任何页面。更好的方法是让控件公开页面本身(母版页或普通页面)将用于设置和检索值的属性和事件。举个简单的例子:
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)
{
}
}
使用 Page 的 Master 属性访问它的母版页。之后,您可以使用 FindControl 方法或使用 master 的公共属性(如果有)。例如后面的母版页代码:
public Label Title { get { return lblTitle; } }