2012-04-15 34 views
0

我的主頁面中的自定義控件,它可以訪問主頁面嗎?關於ASP.NET中的Web控制範圍的困惑

請幫忙! 這是我的代碼。

namespace TVSSystem 
{ 
    public partial class ControlTVS1 : System.Web.UI.UserControl 
    { 
     Page abc; 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      abc = this.Page; //Control: I suposse that I can access all controls of my page 
     } 


     protected void Image1_Click(object sender, ImageClickEventArgs e) 
     { 
     ContentPlaceHolder cph = (ContentPlaceHolder)abc.FindControl("ContentPlacerHolder1"); 

     TextBox txt = (TextBox)cph.FindControl("TextBox1"); 
     Button btn = (Button)cph.FindControl("Button3"); 
     txt.Visible = true; 
     btn.Visible = true; 
     } 
    } 
} 

已解決。 http://forums.asp.net/t/1000865.aspx/1

回答

2

您可以使用Page屬性來訪問包含該用戶控件的頁(不管你把它放在一個母版中):

public partial class WebUserControl1 : System.Web.UI.UserControl 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     var page = this.Page; 
     ... 
    } 
} 
+0

我這樣做了,但拋出NullReferenceException異常。 – 2012-04-15 08:39:16

+0

你更精確地做了什麼?你能展示一些代碼嗎?我很難猜測。 – 2012-04-15 08:40:09

+0

現在您可以看到我的代碼。 – 2012-04-15 08:45:43