2015-10-16 10 views
0

我無法在for-each期間找到自定義用戶控件。無法在foreach中看到我的用戶控件

這是一個自定義用戶控件,這裏是我的代碼:

  <uc2:MetaTag runat="server" id="MetaTag" SystemCode="TEXT" Text="test" /> 

但它確實找到文本控件,我有。

 foreach (Control controlItemFound in Page.Controls) 
     { 
      Response.Write("Control Found: " + controlItemFound.GetType() + "<br); 
      if(controlItemFound is MetaTag) 
      { 
       MetaTag ctrl = (MetaTag)controlItemFound; 

       ctrl.Text = Server.HtmlDecode("MetaTag FOUND!!!"); 
      } 
      if (controlItemFound is LiteralControl) 
      { 
       LiteralControl ctrl = (LiteralControl)controlItemFound; 

       ctrl.Text = Server.HtmlDecode("Server Text!"); 
      } 
     } 

回答

1

Page.Controls將只允許您直接訪問頁面Class中的控件。它不包括嵌套在其他控件中的控件。 例如,您可能添加到Page(嵌套在Form標記中)的Literal和Meta控件將不會包含在Page.Controls集合中。

至於在Page.Controls集合中已經識別的LiteralControl,這將是ASP.NET框架添加的LiteralControl之一。

嘗試遍歷Meta控件容器的Controls屬性。 如果元控制表單(form標籤)直接子,

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <uc2:metatag runat="server" id="MetaTag" systemcode="TEXT" text="test" /> 
    </form> 
</body> 
</html> 

然後使用下面的代碼,其中Form1中,即形式

foreach (Control controlItemFound in this.form1.Controls) 
{ 
} 
+0

很高興我能幫助的名稱 – Don

相關問題