简体   繁体   中英

How to Reload Dynamically added User controls after the Add button click

I have a requirement where I have to load the user control dynamically upon clicking the Add Button which is present in the Parent Page. The user control contains Text boxes, Dropdowns and a remove button. If I click Remove button of the particular User control it gets removed. Everything is working fine with that. The problem is, upon clicking the Add Button, the page post backs and the selected values in the user control looses its values. I need a way to get all the dynamically added controls from Code behind and store it in a view state and in the page load again convert it into user control and add it to the page. Kindly help with this.

SecondaryDestination.ascx would look like this The place holder will load controls dynamically.

<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>

The user controls add controls dynamically

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);

            }

This User control will be added in 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>

Adding and removing user controls works fine:

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

My problem is I need to save the selected items in the previously added user control's dropdown each time the Add button is clicked to add new instance of user control added dynamically

When the viewstate is loaded during the postback processing, it associates the values in the view states with the controls that have been loaded. You have to reload the control dynamically before the viewstate is processed. I've done this in the past during the page_init event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM