占位符在UpdatePanel中

时间:2010-11-10 15:25:33

标签: asp.net vb.net

提供一些背景的小伪代码:

  1. 我有一个带占位符和按钮的ASPX
  2. 单击该按钮时,会将Web用户控件(uc1)添加到我的占位符
  3. uc有一个按钮
  4. 单击时,会向占位符
  5. 添加不同的用户控件(uc2)

    单步执行代码,如果我在#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。

1 个答案:

答案 0 :(得分:1)

首先,使用Parent.FindControl而不是这个混乱的foreach-es。