さて、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>
ここで作成するサンプル セルは、コード ビハインドに追加する他のセルのテンプレートとしてのみ使用しているため、表示されません。
TableCell cell = new TableCell();
TableRow row = new TableRow();
cell = exampleCell;
row = attachedFilesRow;
((Label)cell.Controls[0]).Text = fileName;
cell.Visible = true;
row.Cells.Add(cell);
attachedFilesRow = row;
attachedFilesRow.DataBind();
コード ビハインドのコードは、ASP ページのボタンの onClick メソッドにあります。ユーザーが添付ファイルを追加したい場合、[添付ファイルの追加] をクリックすると、既存のテーブルにセルが追加されます。セルはこの [filenName.txt X] のように見えるはずです。ここで、X は ImageButton であるため、ユーザーはそれをクリックして添付ファイルを削除できます。
私の問題は、ユーザーが添付ファイルを追加するたびに、以前の TableCell が TableRow に追加されるのではなく、新しい TableCell に置き換えられることです。TableCell を作成し、それを既存のテーブルセルインスタンスと等しくなるように設定していたためだと思ったので、これを試しました:
ASP:
<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);
また、attachedFilesRow.DataBind(); を追加して両方の方法を試しました。最後まで、ASP ページから既存のものを直接使用する代わりに、新しい行オブジェクトとセル オブジェクトをインスタンス化する両方の方法を試しました。これらのことはどちらも大きな役割を果たしているようには見えません。
tl;drボタンクリックで既存のテーブルに TableCell を追加するたびに、テーブルに追加するのではなく、テーブル内の他のセルを置き換えます。
編集:
必ずしも新しい行を追加したいわけではありません...これが私が欲しいものです:
ユーザーが 1 つの添付ファイルを追加した後: [fileName.txt X]
2 つ後: [fileName.txt X] [fileName2.txt X]
3 つ後: [fileName.txt X][fileName2.txt X][fileName3.txt X]
これは基本的に行テーブルであり、意味がある場合は列またはセルを追加します。