我有一个要求,我必须在单击Add按钮,它存在于父页面动态加载用户控件。用户控件包含文本框,下拉菜单和删除按钮。如果我点击特定用户控件的删除按钮,它将被删除。一切工作正常。 问题是,在点击添加按钮时,页面后背上,所选择的值在用户控制失去其价值。我需要一种方法从代码中获取所有动态添加的控件,并将其存储在视图状态中,并在页面加载时再次将其转换为用户控件并将其添加到页面中。请帮忙。添加按钮后如何刷新动态添加用户控件单击
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是在动态加载
,而不是从代码添加控件后面为什么不显示CSS属性改变这样'显示:无;' –
我不能使用CSS,因为将被添加用户控件的数量是在一开始不明。点击添加按钮后,我需要添加n个用户控件。 – user3756357
@ Sid:我可以发布图片以更好地理解我的任务。但是我还没有足够的声望去做这件事。 – user3756357