C#.net如何通过代码动态添加图像到表格单元格

问题描述 投票:0回答:2

我正在尝试将图像添加到我通过后面的代码动态制作的表格中。 但是图像不会添加到单元格中。 我发现的所有其他代码都在讨论 htmltable,它与 table() 不同;

public partial class Children_nihul_1 : System.Web.UI.Page
{
    protected string mychildrenlist;

    protected void Page_Init(object sender, EventArgs e)
    {
        var cases = new List<string>();
        System.Web.UI.WebControls.Image myGamepic1 = new System.Web.UI.WebControls.Image();
       // var myGamepic1 = new Image();
        myGamepic1.ImageUrl = "~/img/inner-page/Game1.png";
        myGamepic1.Height = 30;

//这部分只是从数据库中调用数据-与 问题。/

        DataSet ds = new DataSet();
        string x = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("./App_Data/DatabaseGames.accdb") + ";";
        string y = "select * from Students";

        mySql myq = new mySql();
        ds = myq.sqlRet(x, y);
        rowlist = ds.Tables[0].Rows.Count;

        for (int i = 0; i < rowlist; ++i)
        {
            cases.Add(ds.Tables[0].Rows[i][1].ToString()); 
        }

//我的表创建

        Table table = new Table();
        table.BorderStyle = BorderStyle.None;

        //Add Header Row
        TableRow row = new TableRow();
        TableHeaderCell headerCell = new TableHeaderCell();
        headerCell.Text = "aaa";

        row.Controls.Add(headerCell);
        headerCell = new TableHeaderCell();
        headerCell.Text = "ccc";
        row.Controls.Add(headerCell);

        table.Controls.Add(row);

        for (int i = 0; i < rowlist; ++i)
        {
            //Add DataRow
            row = new TableRow();
            TableCell cell = new TableCell();
            cell.Text = cases[i];
            row.Controls.Add(cell);

            cell = new TableCell();
            cell.Controls.Add(myGamepic1);

            row.Controls.Add(cell);

            table.Controls.Add(row);
        }

        Panel1.Controls.Add(table);
        Panel1.Controls.Add(myGamepic1);
    }

}
c# asp.net image dynamic
2个回答
3
投票

尝试这样的事情。使用代码隐藏添加图像动态时对我有用。

cell11.Text = string.Format("<img src='images/close_icon.png' />");

0
投票

这对我有用

TableRow tr = new TableRow();
TableCell tc = new TableCell();
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image()
{
  ImageUrl = "~/Resources/myImage.bmp"
};

tr.Cells.Add(tc);
tr.Cells.Add(new TableCell() {
   Controls = {img}
});

this.myTable.Rows.Add(tr);

从这里:将图像添加到表格单元格

© www.soinside.com 2019 - 2024. All rights reserved.