2011-06-17 44 views
1

我有一箇中繼器列出了客戶項目,其中一個是用於獲取更多信息的按鈕。當用戶點擊該按鈕時,用戶控件就會出現在其下方。我希望用戶控件在再次單擊按鈕時關閉。我最初切換了服務器端的可見性,但是現在我無法使用它,因爲需要通過參數將用戶控件加載到按鈕單擊上。我也不能使用jQuery顯示和隱藏,因爲這將涉及到整個頁面首先下載所有數據,這可能會使頁面太麻煩。FindControl無法找到我的動態添加的控件

所以目前在每個中繼器我有命令參數和按需事件一個LinkBut​​ton的時候觸發這個事件:

protected void uxPolicyDocsButton_Command(object sender, CommandEventArgs e) 
    { 
     //Find Policy Summary control within repeater item in order to toggle visibility 

     LinkButton button = sender as LinkButton; 
     if (button != null) 
     { 
      RepeaterItem ri = button.Parent as RepeaterItem; 

      if (ri != null) 
      { 
       PlaceHolder policySummaryPlaceHolder = (PlaceHolder)ri.FindControl("uxPolicySummaryPlaceHolder");      
       Control policyDocuments = (Control)PolicySummaryPlaceHolder.FindControl("uxPolicyDocumentsControl"); 


       foreach (Control c in policySummaryPlaceHolder.Controls) 
       { 
        //FindControl action goes in here. Have stepped through though and it doesn't appear 
       } 

       if (policyDocuments != null) 
       { 
        policySummaryPlaceHolder.Controls.Remove(policyDocuments); 
       } 
       else 
       { 
        policyDocuments uxPolicyDocumentsControl = (PolicyDocuments)LoadControl("~/Controls/Home/PolicyDocuments.ascx"); 
        uxPolicyDocumentsControl.PolicyNumber = button.CommandArgument; 
        policySummaryPlaceHolder.Controls.Add(uxPolicyDocumentsControl); 
       } 
      } 
     } 
    } 

我對切換計劃是,如果PolicyDocuments控制爲空,然後加載控制,如果沒有,則刪除該控件,但它總是返回null。它雖然正確加載控件。

這裏是轉發器的部分:

<ItemTemplate> 
        <tr> 
         <td> 
          <%#Eval("StartDate","{0:d}")%> 
         </td> 
         <td class="center-cell-ctrl"> 
          Postcode:<br /> 
          <%#Eval("Postcode")%> 
         </td> 
         <td id='<%#Eval("PolicyNumber")%>' class="button-cell">         
          <asp:LinkButton ID="uxPolicyDocsButton" CommandName="PolicyNumber" CommandArgument='<%#Eval("PolicyNumber")%>' OnCommand="uxPolicyDocsButton_Command" runat="server" Visible="false">Policy<br />Documents</asp:LinkButton> 
         </td> 
        </tr> 
        <asp:PlaceHolder ID="uxPolicySummaryPlaceHolder" runat="server"></asp:PlaceHolder> 
        <asp:PlaceHolder ID="uxPolicyDocumentsPlaceHolder" runat="server"></asp:PlaceHolder> 

     </ItemTemplate> 

我有一個到處尋找誰也有類似問題的人,和一個共同的答案是,你需要加載在Page_Init事件控制。這是否意味着這種方式不起作用?如果是這樣的話,任何替代方案的想法?

非常感謝

+0

在用戶控件中創建控件嗎? – Andrew 2011-06-17 10:41:28

回答

1

我不明白」,爲什麼你不能添加PolicyDocumentsvisible = false並沒有數據源,而不是Placeholder,這樣的:

<uc:PolicyDocuments ID="uxPolicyDocuments" runat="server" Visible="false"></asp:PolicyDocuments> 

,並在命令使用這樣的代碼:

 if (ri != null) 
     { 
      var policyDocuments = ri.Controls.OfType<PolicyDocuments>().FirstOrDefault(); 
      if (policyDocuments == null) 
       return; 

      if (policyDocuments.Visible) 
      { 
       policyDocuments.Visible = false; 
      } 
      else 
      { 
       policyDocuments.PolicyNumber = button.CommandArgument; 
       policyDocuments.Visible = true; 
      } 
     } 

在這種情況下,你不需要在Init活動頁面使用一些黑客。 無論如何,您始終可以使用.Controls.OfType<PolicyDocuments>()擴展方法來獲取所需的所有用戶控件。

3

每次你的頁面加載時間(無論是最初,或通過回傳)控制樹需要被重新創建。

您的頁面的ASPX部分中聲明的控件由框架(種類)添加,但您添加的任何控件「動態」都需要放回控制樹中。如果他們不是,那麼就像你發現的那樣,他們根本不能再被發現。

複雜的控件,如datagridview,通過在ViewState中存儲(大量的)數據來重建其控制樹。

它可能會更簡單,而不是讓您的頁面添加/刪除PlaceHolder中的項目,創建一個您可以隱藏的用戶控件,並在第一次顯示時加載它的數據。這也有助於你控制和頁面堅持Single Repsonsibility Principal

相關問題