如何在“添加”按钮单击后重新加载动态添加的用户控件

时间:2014-06-19 14:16:36

标签: c# asp.net .net custom-controls webusercontrol

我有一个要求,我必须在单击父页面中的添加按钮时动态加载用户控件。用户控件包含文本框,下拉列表和删除按钮。如果我单击特定用户控件的“删除”按钮,则会将其删除。一切都很顺利。 问题是,在单击“添加”按钮时,页面后退并且用户控件中的选定值将丢失其值。我需要一种方法来从代码中获取所有动态添加的控件并将其存储在视图状态中,并在页面加载中再次将其转换为用户控件并将其添加到页面中。请帮助。

SecondaryDestination.ascx看起来像这样 占位符将动态加载控件。

<td><asp:Label id="lblSecondary">Secondary Destination</asp:Label></td>     
<td>asp:DropDownList id="ddlSecondary" runat="server" CssClass="ddlbox" AutoPostBack="true"></asp:DropDownList> </td>   
<asp:UpdatePanel ID="updateEmailFtp" runat="server" UpdateMode="Conditional">
<ContentTemplate>
    <asp:PlaceHolder ID="plcEmail" runat="server">
        <table cellpadding="0" cellspacing="0" border="0" class="tbl_form" runat="server" id="tblSecondary">
        </table>    
    </asp:PlaceHolder>
</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="ddlSecondary" EventName="SelectedIndexChanged" />
</Triggers>

用户控件动态添加控件

protected void ddlSecondary_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(ddlSecondary.SelectedIndex > 0)
        {
            pickerSecondaryViewPresenter = new PickerSecondaryViewPresenter(this);
            DestinationBO secondaryDestination = pickerSecondaryViewPresenter.getSecondaryDestination(int.Parse(ddlSecondary.SelectedItem.Value));

            if(secondaryDestination.DestinationType == "Email")
            {
                HtmlTable tblContent = (HtmlTable)FindControl("tblSecondary");

                Label lblPrimary = new Label();                 
                lblPrimary.Text = "Email Disstribution List";                                       
                HtmlTableCell tcPrimary1 = new HtmlTableCell();
                tcPrimary1.Controls.Add(lblPrimary);

                Label lblPrimaryDistributionList = new Label(); 
                lblPrimaryDistributionList.Text = secondaryDestination.DistributionList;
                HtmlTableCell tcPrimary2 = new HtmlTableCell();
                tcPrimary2.Controls.Add(lblPrimaryDistributionList);

                HtmlTableRow trPrimary = new HtmlTableRow();
                trPrimary.Controls.Add(tcPrimary1);
                trPrimary.Controls.Add (tcPrimary2);                        
                tblContent.Controls.Add(trPrimary);                     
            }
            else
            {
                Label lblFtp = new Label();                 
                lblFtp.Text = "Ftp Host";
                HtmlTableCell tcPrimary1 = new HtmlTableCell();
                tcPrimary1.Controls.Add(lblFtp);

                Label lblFtpHost = new Label();                 
                lblFtpHost.Text = secondaryDestination.FtpHost;                 
                HtmlTableCell tcPrimary2 = new HtmlTableCell();
                tcPrimary2.Controls.Add(lblFtpHost);    

                Button btn = new Button();
                btn.Text = "Connect FTP";
                tcPrimary2.Controls.Add(btn);   

                HtmlTableRow trPrimary1 = new HtmlTableRow();
                trPrimary1.Controls.Add(tcPrimary1);
                trPrimary1.Controls.Add (tcPrimary2);   

                Label lblDirectory = new Label();                   
                lblDirectory.Text = "Ftp Directory";
                HtmlTableCell tcPrimary3 = new HtmlTableCell();
                tcPrimary3.Controls.Add(lblDirectory);

                TextBox txtDirectory = new TextBox();                   
                txtDirectory.Text = "";                 
                HtmlTableCell tcPrimary4 = new HtmlTableCell();
                tcPrimary4.Controls.Add(txtDirectory);

                HtmlTableRow trPrimary2 = new HtmlTableRow();
                trPrimary2.Controls.Add(tcPrimary3);
                trPrimary2.Controls.Add(tcPrimary4);

                tblSecondary.Controls.Add(trPrimary1);      
                tblSecondary.Controls.Add(trPrimary2);

            }

此用户控件将添加到page1.aspx:

<div>
<asp:UpdatePanel id ="up1" UpdateMode="conditional">
    <ContentTemplate>
        <uc:DestinationPicker id="destinatioPicker" runat="server" ></uc:DestinationPicker>
    </ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:PlaceHolder ID="divSecondary" runat="server">  
    <ucS:DestinationPickerSecondary id="destinationPickerSecondary" runat="server" ></ucS:DestinationPickerSecondary>       
</asp:PlaceHolder>  

<div>
    <asp:Button id="btnAddSecondary" Text="Add Secondary" runat="server" OnClick="btnAddSecondary_Click"></asp:Button>
</div>

</div>

添加和删除用户控件可以正常工作:

protected void Page_Load(object sender, EventArgs e)
{    if(IsPostBack)
{
    AddAndRemoveDynamicControls();
}
}

我的问题是,每次单击“添加”按钮添加动态添加的新用户控件实例时,我需要在先前添加的用户控件的下拉列表中保存所选项目

1 个答案:

答案 0 :(得分:1)

在回发处理期间加载视图状态时,它会将视图状态中的值与已加载的控件相关联。您必须在处理视图状态之前动态重新加载控件。我在page_init事件期间过去做过这个。