提供一些背景的小伪代码:
单步执行代码,如果我在#4按钮单击之前和之后查看placeholder.controls.count,则计数会增加1,如您所料。
问题是#4中添加的uc2没有出现在屏幕上。所以,我将占位符包装在UpdatePanel中。我以前从未使用过。所以,我可以在添加uc2之后刷新占位符。
相关的ASPX代码
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server></ajaxToolkit:ToolkitScriptManager>
<ajaxToolkit:TabContainer ID="tcNavigation" runat="server" ActiveTabIndex="0">
<ajaxToolkit:TabPanel ID="tpWebMerchandising" runat="server" HeaderText="WEB">
<ContentTemplate>
<table cellpadding="3" cellspacing="0" border="0">
<tr>
<td valign="top" class="label">Attributes</td>
<td>
<asp:UpdatePanel runat="server" id="UpdatePanel1" updatemode="Conditional">
<ContentTemplate>
<asp:PlaceHolder ID="phAttributes" runat="server"></asp:PlaceHolder><br />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnAddAttribute" runat="server" Text="Add Attribute" /> | <asp:Button ID="btnCreateAttribute" runat="server" Text="Create Attribute" />
</td>
</tr>
</table>
</ContentTemplate>
</ajaxToolkit:TabPanel>
</ajaxToolkit:TabContainer>
相关的ASCX代码
For Each ctrl As Control In Parent.Page.Controls
For Each ctrl2 As Control In ctrl.Controls
For Each ctrl3 As Control In ctrl2.Controls
For Each ctrl4 As Control In ctrl3.Controls
For Each ctrl5 As Control In ctrl4.Controls
If InStr(ctrl5.GetType.ToString(), "UpdatePanel") > 0 Then 'UPDATE PANEL
up = ctrl5
End If
For Each ctrl6 As Control In ctrl5.Controls 'CONTENT TEMPLATE
For Each ctrl7 As Control In ctrl6.Controls 'PLACE HOLDER
If ctrl7.GetType.ToString() = "System.Web.UI.WebControls.PlaceHolder" Then
phAttributes = ctrl7
End If
Next
Next
Next
Next
Next
Next
Next
Dim ctrlAttributes As New AttributeControl
ctrlAttributes.AttributeName = attrName
ctrlAttributes.AttributeValue = attrValue
ctrlAttributes.ID = "ctrlAttribute-" & attrName
phAttributes.Controls.Add(ctrlAttributes)
我试过了......
up.Update()
这......
Me.Page.RegisterStartupScript("AutoPostBackScript", "__doPostBack('UpdatePanel1', '');")
目标是刷新占位符,以便在屏幕上显示新的ctrlAttributes。
答案 0 :(得分:1)
首先,使用Parent.FindControl而不是这个混乱的foreach-es。