【问题标题】:Dynamically adding images using UpdatePanel使用 UpdatePanel 动态添加图像
【发布时间】:2013-10-22 23:03:40
【问题描述】:

我正在尝试在表格中动态创建图像,但似乎 UpdatePanel 没有更新。我的 .aspx 页面中有以下代码。

<%@ 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>

然后这是 aspx.cs 页面。

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

我知道图像被正确调用,因为我已经使用客户端上的图像标签手动输出了它。任何帮助都会很棒!谢谢

【问题讨论】:

    标签: c# asp.net image updatepanel asp.net-placeholder


    【解决方案1】:

    你需要先添加表格,添加行,最后添加表格到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);
    

    【讨论】:

    • 工作!!感谢您的帮助!
    【解决方案2】:

    您可以使用 javascript 和 Web 服务来执行此操作。

    创建一个调用 Web 服务的函数并获取图像的 url 并将其附加到更新面板 div 中

    【讨论】:

      猜你喜欢
      • 2014-05-31
      • 2013-11-17
      • 2018-06-07
      • 1970-01-01
      • 2015-12-13
      • 2017-09-12
      • 2014-07-05
      • 2018-08-28
      • 1970-01-01
      相关资源
      最近更新 更多