[英]How to Reload Dynamically added User controls after the Add button click
我有一个要求,当我单击父页面中的“添加”按钮时,必须动态加载用户控件。 用户控件包含文本框,下拉菜单和删除按钮。 如果单击特定用户控件的“删除”按钮,它将被删除。 一切都很好。 问题是,单击“添加”按钮后,页面将回退,并且用户控件中的选定值将失去其值。 我需要一种方法来从Code后面获取所有动态添加的控件,并将其存储在视图状态中,然后在页面加载中再次将其转换为用户控件,并将其添加到页面中。 请对此提供帮助。
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();
}
}
我的问题是,每次单击添加按钮以添加动态添加的用户控件的新实例时,我都需要将所选项目保存在先前添加的用户控件的下拉列表中
在回发处理期间加载viewstate时,它将视图状态中的值与已加载的控件相关联。 您必须在处理viewstate之前动态地重新加载控件。 我过去在page_init事件中完成了此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.