C# 回发时,将替换动态添加的控件

C# 回发时,将替换动态添加的控件,c#,asp.net,C#,Asp.net,好的,我的ASP页面上有一个表,如下所示: <asp:Table ID="attachedFiles" runat="server" BorderColor="LightGray" BorderWidth="1" ViewStateMode="Enabled"> <asp:TableRow ID = "attachedFilesRow" runat="server"> <asp:TableCell ID="exampleCell" runa

好的,我的ASP页面上有一个表,如下所示:

<asp:Table ID="attachedFiles" runat="server" BorderColor="LightGray" BorderWidth="1" ViewStateMode="Enabled"> 
    <asp:TableRow ID = "attachedFilesRow" runat="server">
        <asp:TableCell ID="exampleCell" runat="server" Visible="false" >
            <asp:Label runat="server" Text="" />
            <asp:ImageButton runat="server" ImageAlign="Middle" ImageUrl="~/images/kill.png" OnClick="removeAttachment" />
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>
代码隐藏中的代码位于ASP页面上按钮的onClick方法中。其思想是,如果用户想要添加附件,他们单击“添加附件”,然后将一个单元格添加到已经存在的表中。该单元格应该看起来像[filenName.txt X],其中X是一个ImageButton,因此用户可以单击它并删除附件

我的问题是,每次用户添加附件时,以前的TableCell都会替换为新的TableCell,而不是添加到TableRow。我想可能是因为我创建了一个TableCell,然后将其设置为等于一个已经存在的TableCell实例,所以我尝试了以下方法:

ASP:

我还尝试了添加AttachedFileRow.DataBind()的两种方法;最后,我尝试了两种方法,实例化新的行和单元格对象,而不是直接使用ASP页面中的现有对象。这两件事似乎都没有起多大作用

tl;dr每当我在按钮单击时向现有表格中添加一个表格单元格时,它都会替换表格中的另一个单元格,而不是添加到表格中

编辑:

我不一定要添加新行。。。这就是我想要的:

用户添加一个附件后:[fileName.txt X]

两个之后:[fileName.txt X][fileName2.txt X]

三次之后:[fileName.txt X][fileName2.txt X][fileName3.txt X]

它基本上是一个在行表,如果有意义的话,可以添加列或单元格。

您不能“替换上一个单元格”。在每一篇文章后面,你都会得到一个新的页面实例。这是因为web是无状态的,尽管ASP.NET给人以状态的印象,但它实际上并不存在

所以,每次你都是从头开始,总是在你的行中创建一个单元格。您需要做的是保留一个已添加的所有项的列表,然后每次呈现它们(通过数据绑定或代码)

此示例应该完全满足您的要求:

namespace WebApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;

    public partial class _Default : System.Web.UI.Page
    {
        private const string UploadViewState = "UploadViewState";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // initialise list and store in ViewState
                ViewState[UploadViewState] = new List<string>();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            // Get the list from ViewState and add the new item
            var _filesToUpload = (List<string>)ViewState[UploadViewState];
            _filesToUpload.Add(text1.Text);

            // Now recreate the row adding cells for each file...
            foreach (string item in _filesToUpload)
            {
                TableCell cell = new TableCell();
                ImageButton button = new ImageButton();
                button.ImageUrl = "~/images/kill.png";
                button.Attributes.Add("onclick", "removeAttachment");
                Label label = new Label();
                label.Text = item;
                cell.Controls.Add(button);
                cell.Controls.Add(label);
                attachedFilesRow.Cells.Add(cell);
            }
        }
    }
}
命名空间WebApplication1
{
使用制度;
使用System.Collections.Generic;
使用System.Web.UI.WebControl;
公共部分类\u默认值:System.Web.UI.Page
{
私有常量字符串UploadViewState=“UploadViewState”;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
//初始化列表并存储在ViewState中
ViewState[UploadViewState]=新列表();
}
}
受保护的无效btnAdd_单击(对象发送者,事件参数e)
{
//从ViewState获取列表并添加新项
var_filesToUpload=(列表)ViewState[UploadViewState];
_filesToUpload.Add(text1.Text);
//现在重新创建行,为每个文件添加单元格。。。
foreach(在_filesToUpload中的字符串项)
{
TableCell=新的TableCell();
ImageButton=新建ImageButton();
button.ImageUrl=“~/images/kill.png”;
Add(“onclick”、“removeAttachment”);
标签=新标签();
label.Text=项目;
单元格.控件.添加(按钮);
单元格.控件.添加(标签);
AttachedFileRow.Cells.Add(单元格);
}
}
}
}

除了我的代码示例之外,您还可以在文件的
列表中使用数据绑定,但我只是从您的代码中继续。秘诀是使用项目的
列表
并存储在ViewState中,从这一点上讲,其他一切都应该很简单。

您需要修改codebehind中的AttachedFile,而不是attachedFileRow。您需要将行添加到表中,而不是将单元格添加到表行中。就像@Pete的回答,参考:伙计们,你认为你走错了路。它没有出现并且似乎被替换的原因是您动态创建的控件正在丢失!请查看我的答案,找到一个扩展我意思的链接!是的,我使用了Pete和Michael_B的建议,仍然遇到同样的问题,尽管我每次都创建并添加一整行,而不仅仅是一个单元格。我会调查你的答案,贝洛吉克斯。我会回来的。我已经添加了一些代码来澄清我的评论。
<asp:Table ID="attachedFiles" runat="server" BorderColor="LightGray" BorderWidth="1" ViewStateMode="Enabled"> 
    <asp:TableRow ID = "attachedFilesRow" runat="server">
    </asp:TableRow>
</asp:Table>
TableCell cell = new TableCell();

ImageButton button = new ImageButton();
button.ImageUrl = "~/images/kill.png";
button.Attributes.Add("onclick", "removeAttachment");

Label label = new Label();
label.Text = fileName;

cell.Controls.Add(button);
cell.Controls.Add(label);

attachedFilesRow.Cells.Add(cell); 
namespace WebApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;

    public partial class _Default : System.Web.UI.Page
    {
        private const string UploadViewState = "UploadViewState";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // initialise list and store in ViewState
                ViewState[UploadViewState] = new List<string>();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            // Get the list from ViewState and add the new item
            var _filesToUpload = (List<string>)ViewState[UploadViewState];
            _filesToUpload.Add(text1.Text);

            // Now recreate the row adding cells for each file...
            foreach (string item in _filesToUpload)
            {
                TableCell cell = new TableCell();
                ImageButton button = new ImageButton();
                button.ImageUrl = "~/images/kill.png";
                button.Attributes.Add("onclick", "removeAttachment");
                Label label = new Label();
                label.Text = item;
                cell.Controls.Add(button);
                cell.Controls.Add(label);
                attachedFilesRow.Cells.Add(cell);
            }
        }
    }
}