【问题标题】:How to Reload Dynamically added User controls after the Add button click如何在单击添加按钮后重新加载动态添加的用户控件
【发布时间】:2014-08-10 02:56:50
【问题描述】:

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

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

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

【问题讨论】:

  • 而不是从代码中添加控件,为什么不像这样更改显示 CSS 属性 display:none;
  • 我不能使用css,因为一开始将添加的用户控件的数量是未知的。单击添加按钮后,我需要添加 n 个用户控件。
  • @Sid :我本可以发布图片以便更好地理解我的问题。但我还没有足够的声誉来做那件事。

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


【解决方案1】:

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

【讨论】:

  • 是的,我也需要这样做。但是我也需要重新获得用户控件内的控件的值。所以我需要将这些值存储在会话或视图状态中,以便我可以再次绑定它们。我需要一个 wway 在受保护的 void btnAddSecondary_Click(object sender, EventArgs e) { } 下访问它
  • 值保存在视图状态中。问题是回发期间不存在动态添加的用户控件。它们必须在每次回发期间动态添加才能保留。但是,通常发生的情况是在加载 ViewState 之后加载它们,并将值分配给控件。您需要在 Page_Init 中再次动态添加控件,以便在加载视图状态时将值分配给控件。
  • 感谢您的指导@user3754182。你能帮我提供示例代码吗?基本上在 AddAndRemoveDynamicControls() 我添加用户控件如下: Control c = GetPostBackControl(Page);
  • 您需要将 AddAndRemoveDynamicControls() 移至 Page_Init()。您只需要在处理视图状态之前添加动态控件。这不是做正确的事,而是在正确的时间做事。
猜你喜欢
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多