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