我有一个中继器列出了客户项目,其中一个是用于获取更多信息的按钮。当用户点击该按钮时,用户控件就会出现在其下方。我希望用户控件在再次单击按钮时关闭。我最初切换了服务器端的可见性,但是现在我无法使用它,因为需要通过参数将用户控件加载到按钮单击上。我也不能使用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