简体   繁体   中英

C#.net how to add an image into a table cell dynamically by code

I am trying to add an image into a table that I'm making dynamically by code behind. However the image is not added to the cell. All the other codes I'm finding are talking about an htmltable which is not the same as 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;

//this part is only a call for data from database- not relavent to the qustion./

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

//my table creation

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

}

Try something like this. Works for me when adding an image dynamic using code behind.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM