动态地将图像添加到html表中

时间:2011-01-31 08:24:58

标签: c# html asp.net html-table

我正在尝试动态地将图像添加到表中但不会发生... 当我将图像添加到单元格时,也会出现错误消息:

  

无法获取内容,因为内容不是文字。

        if (filteredFileList.Count != 0 && filteredFileList != null) {
            imgProduct.ImageUrl = Server.MapPath(@"/ysyp/Images/Products/") + filteredFileList[0].Name;
            rowCount = 1;
            columnCount = filteredFileList.Count;
            try {
                HtmlTable tbProductImage = new HtmlTable();
                HtmlTableRow trImageRow = new HtmlTableRow();
                for (int j = 0; j < columnCount; j++) {
                    if (filteredFileList.Count != 0) {
                        HtmlTableCell tdImageRow = new HtmlTableCell();
                        Image imageProduct = new Image();                       
                        imageProduct.ID = "img" + filteredFileList[j].Name.Substring(0, filteredFileList[j].Name.LastIndexOf("."));
                        imageProduct.ImageUrl = Server.MapPath(@"/ysyp/Images/Products/") + filteredFileList[j].Name;
                        tdImageRow.Controls.Add(imageProduct);
                        trImageRow.Controls.Add(tdImageRow);
                    }
                }

                tbProductImage.Controls.Add(trImageRow); // <<< ERROR HERE

                tdProduct.Controls.Add(tbProductImage);
            } catch (Exception exc) {
                string msg = exc.Message;
            }
        }

我该如何解决?

5 个答案:

答案 0 :(得分:2)

试试这个:

HtmlTable tbProductImage = new HtmlTable();
HtmlTableRow trImageRow = new HtmlTableRow();
for (int j = 0; j < columnCount; j++)
{
    if (filteredFileList.Count != 0)
    {

        HtmlTableCell tdImageCell = new HtmlTableCell();
        Image imageProduct = new Image();
        imageProduct.ID = "id";
        imageProduct.ImageUrl = "url";
        tdImageCell.Controls.Add(imageProduct);
        trImageRow.Cells.Add(tdImageCell);
    }
}
tbProductImage.Rows.Add(trImageRow);

答案 1 :(得分:1)

你不应该使用Controls.Add,而是使用Rows.Add,试试这个:

tbProductImage.Rows.Add(trImageRow);

但我不确定这是否能解决你的问题。

答案 2 :(得分:0)

试试这个:

tbProductImage.Rows.Add(trImageRow);

tbProductImageHtmlTable。如果要向表中添加HtmlTableRow,则需要将其添加到Rows集合中。将其添加到Controls集合将为您提供当前看到的错误。因此,将代码从Controls更改为Rows应该可以满足您的需求。

同样适用于将行添加到行中。上面几行,您想要将trImageRow.Controls.Add(tdImageRow);替换为trImageRow.Cells.Add(tdImageRow);

答案 3 :(得分:0)

向该行添加一个单元格,然后向该单元格添加一个控件。

我们做这种事......

Dim r As TableRow
    Dim c As TableCell
    Dim lit As Literal

    Me.displayTable.Rows.Clear()

    r = AddRow(Me.displayTable, 0)
    c = AddCell(r, 100)

    lit = New Literal
    lit.Text = "Thank you for registering your interest."
    c.Controls.Add(lit)

Table,Row和Cell对象来自System.Web.UI.WebControls命名空间,但它们呈现为HTML表格。

答案 4 :(得分:0)

您正尝试将图片添加到tablerowTR)这不起作用,因为TR只能包含tablecells({{ 1}})。 您试图直接将行添加到表中而不是表TD集合:

这样做的方法是:

  1. 换个新行。
  2. 制作新单元格。
  3. 将图像添加到新单元格。
  4. 将新单元格添加到新行。
  5. 向表中添加新行(通过表的Rows连接)
  6. 编辑:对不起,我的视力不是这样 - 老年和深夜: - (

    我认为您的代码越野了,因为您没有将行添加到表的Rows集合中:

    <击> tbProductImage.Controls.Add(trImageRow); //&lt;&lt;&lt;错误在这里     tbProductImage.Rows.Add(trImageRow);     tdProduct.Controls.Add(tbProductImage);

    更多信息:http://msdn.microsoft.com/en-us/library/7bewx260(v=VS.85).aspx