简体   繁体   English

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

[英]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. 我需要一种方法来从Code后面获取所有动态添加的控件,并将其存储在视图状态中,然后在页面加载中再次将其转换为用户控件,并将其添加到页面中。 Kindly help with this. 请对此提供帮助。

SecondaryDestination.ascx would look like this The place holder will load controls dynamically. 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>

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: 此用户控件将添加到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. 在回发处理期间加载viewstate时,它将视图状态中的值与已加载的控件相关联。 You have to reload the control dynamically before the viewstate is processed. 您必须在处理viewstate之前动态地重新加载控件。 I've done this in the past during the page_init event. 我过去在page_init事件中完成了此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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