简体   繁体   English

动态添加控件不会以asp.net Web形式显示在面板内部

[英]dynamically adding controls does not displayed inside panel in asp.net web form

I have a web form with a table where one row contains panel to add fileupload controls dynamically 我有一个带有表格的网络表单,其中一行包含用于动态添加文件上传控件的面板

the code is as below 代码如下

      <tr> 
     <td  style="width:70%;display:block; overflow:visible;" >
        <asp:Panel ID="ImagePanel" runat="server">
        <uc1:AddNewImage runat="server" id="AddNewImage" /></asp:Panel>
        <asp:Button ID="AddImage"  OnClick="Unnamed_Click" Text="Add New Image" runat="server" CausesValidation="False" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="Please Enter Model Items" ForeColor="Red" ControlToValidate="TxtItems"></asp:RequiredFieldValidator>
    </td>
    </tr>

In button click event i am adding new usercontrol to ImagePanel using following code 在按钮单击事件中,我正在使用以下代码向ImagePanel添加新的用户控件

        Controls.AddNewImage Obj = (Controls.AddNewImage)LoadControl(@"~/Controls/AddNewImage.ascx");
        this.ImagePanel.Controls.Add(Obj);

Problem is only two controls are displayed inside the panel, i need to allow upto five controls, but these controls are not displayed inside panel. 问题是面板内仅显示两个控件,我最多需要允许五个控件,但这些控件未显示在面板内。 What should i need to do to get displayed these controls inside the panel. 我应该怎么做才能在面板内显示这些控件。 do i need to set any css for Panel. 我是否需要为面板设置任何CSS。 Panel is inside a tag. 面板在标签内。

That's because dynamically added controls are cleared when page PostBack. 这是因为在页面回发时会清除动态添加的控件。 Simply solution is to add <asp:HiddenField id="hidCount" runat="server" value="1"/> and update your click event like this: 简单的解决方案是添加<asp:HiddenField id="hidCount" runat="server" value="1"/>并更新您的click事件,如下所示:

int count = int.Parse(hidCount.Value)++;
hidCount.Value = count.ToString();
for(i=0;i< count;i++) {
        Controls.AddNewImage Obj = (Controls.AddNewImage)LoadControl(@"~/Controls/AddNewImage.ascx");
        this.ImagePanel.Controls.Add(Obj);
}

This will work perfectly. 这将完美地工作。

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

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