Search code examples
c#asp.netimageupdatepanelasp.net-placeholder

Dynamically adding images using UpdatePanel


I'm trying to dynamically create images in a table but it seems like the UpdatePanel is not updating. I have the following code in my .aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ViewTile.aspx.cs" Inherits="ViewTile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body runat="server">
        <form id="form1" runat="server">
            <div>
                <asp:Image ID="tileImg" runat="server"/>
                <asp:ScriptManager ID="ScriptManager1" runat="server"/>
                <table>
                    <asp:updatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                            <asp:PlaceHolder runat="server" ID="placeholder1"/>
                        </ContentTemplate>
                    </asp:updatePanel>
                </table>        
            </div>
        </form>
    </body>
</html>

And then this this the aspx.cs page.

TableRow imageRow = new TableRow();

for (int rowItr = 0; rowItr < 3; rowItr++)
{
    for (int colItr = 0; colItr < 4; colItr++)
    {
        System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
        image.ImageUrl = "~/ShowTile.ashx?SheetId=" + SheetGUID + "&Scale=" + 3
            + "&XCoord=" + colItr + "&YCoord=" + rowItr;
        placeholder1.Controls.Add(image);
        TableCell imageCell = new TableCell();
        imageCell.Controls.Add(image);
        imageRow.Cells.Add(imageCell);
    }

    placeholder1.Controls.Add(imageRow);
    imageRow.Cells.Clear();
}

I know for a fact that the image is being called correctly as I've outputted it manually using an image tag on the client. Any help would be great! Thanks


Solution

  • You need to add Table first, add rows to it and finally add table to placeholder1.Controls

    Table tbl= new Table();
    for (int rowItr = 0; rowItr < 3; rowItr++)
    {
        TableRow imageRow = new TableRow();
        for (int colItr = 0; colItr < 4; colItr++)
        {
    
            System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
            image.ImageUrl = "~/ShowTile.ashx?SheetId=" + SheetGUID + "&Scale=" + 3
                + "&XCoord=" + colItr + "&YCoord=" + rowItr;
            TableCell imageCell = new TableCell();
            imageRow.Cells.Add(imageCell);            
        }
        tbl.Rows.Add(imageRow);
    }
    placeholder1.Controls.Add(tbl);