开发者

Add image into html table dynamically

开发者 https://www.devze.com 2023-02-07 10:00 出处:网络
I\'m trying to add an image into table dynamically but it doesn\'t happen... also when I add an image into the cell there is an error message:

I'm trying to add an image into table dynamically but it doesn't happen... also when I add an image into the cell there is an error message:

Cannot get the inner content of because the contents are not literal.

        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) {
       开发者_如何学JAVA         string msg = exc.Message;
            }
        }

How can I fix it?


Try this:

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


You should not use Controls.Add, use Rows.Add instead, try this:

tbProductImage.Rows.Add(trImageRow);

But I'm not sure if this will solve your problem.


Try this:

tbProductImage.Rows.Add(trImageRow);

tbProductImage is an HtmlTable. If you want to add an HtmlTableRow to the table, you need to add it to the Rows collection. Adding it to the Controls collection will give you the error that you currently see. So changing your code from Controls to Rows should give you what you need.

The same thing applies for adding a cell to a row. A few lines above, you want to replace trImageRow.Controls.Add(tdImageRow); with trImageRow.Cells.Add(tdImageRow);


Add a cell to the row, and then add a control to the cell.

We do this kind of thing...

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)

The Table, Row, and Cell objects are from the System.Web.UI.WebControls namespace, but they render as HTML tables.


You are trying to add the image to a tablerow (TR) this won't work because TRs can only contain tablecells (TD). You are trying to add the row to the table directly instead of to the table's Rows collection:

The way to do it is:

  1. Make a new row.
  2. Make new cell.
  3. Add image to new cell.
  4. Add new cell to new row.
  5. Add new row to table (via table's Rows connection)

EDIT: Sorry, my eyesight isn't what it was - old age and late nights :-(

I think your code goes off-road because you're not adding the row to the table's Rows collection:

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

More info: http://msdn.microsoft.com/en-us/library/7bewx260(v=VS.85).aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消