2

是否可以从位于子页面中的Web用户控件访问主页面变量?我知道你可以通过添加访问它们在子页面下面引用来自子页面控件的masterpage变量?

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

它似乎并不试图从Web控件是一个子页

+0

听起来很脆。你有什么尝试?成员变量?公共财产? – n8wrl 2012-03-08 18:46:54

+0

听起来像一个坏主意。您如何知道您的用户控件将在具有正确主人的页面上使用? – Khan 2012-03-08 18:50:43

回答

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 2012-03-09 16:54:20

0

使用页面的主属性内访问时,工作访问它的主页面。之后,您可以使用FindControl方法或使用主控的公共属性(如果有)。例如,在后面的主网页代码:

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

必须有一种更好的方式来与用户控制进行交流。如果在没有此主人的网页上使用该控件,该怎么办? – n8wrl 2012-03-08 18:48:50

+0

然后,您可以通过使用它的ID(作为变量)或使用页面(或任何其他父控件)的FindControl方法直接访问该页面的控件。之后,您可以使用用户控件的任何公共属性,方法,字段。 – 2012-03-08 18:51:59