我有一箇中繼器列出了客戶項目,其中一個是用於獲取更多信息的按鈕。當用戶點擊該按鈕時,用戶控件就會出現在其下方。我希望用戶控件在再次單擊按鈕時關閉。我最初切換了服務器端的可見性,但是現在我無法使用它,因爲需要通過參數將用戶控件加載到按鈕單擊上。我也不能使用jQuery顯示和隱藏,因爲這將涉及到整個頁面首先下載所有數據,這可能會使頁面太麻煩。FindControl無法找到我的動態添加的控件
所以目前在每個中繼器我有命令參數和按需事件一個LinkButton的時候觸發這個事件:
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事件控制。這是否意味着這種方式不起作用?如果是這樣的話,任何替代方案的想法?
非常感謝
在用戶控件中創建控件嗎? – Andrew 2011-06-17 10:41:28