如何在表格单元格asp.net c#中添加占位符



我想在图像预览列下的Registration表中添加占位符2。我正在获取图像路径值,但无法显示该图像的预览。图像路径存储在数据库中。

if (!this.IsPostBack)
    {
        DataTable dt = this.GetData();
        StringBuilder html = new StringBuilder();
        html.Append("<div class='container'>");
        html.Append("<div class='table-responsive'>");
        html.Append("<table class='table table-striped table-bordered'>");
        html.Append("<thead>");
        html.Append("<tr style='color:Crimson'>");
        html.Append("<th>First Name</th>");
        html.Append("<th>Surname</th>");
        html.Append("<th>Birth Date</th>");
        html.Append("<th>Birth Month</th>");
        html.Append("<th>Birth Year</th>");
        html.Append("<th>Gender</th>");
        html.Append("<th>ID Card Path</th>");
        html.Append("<th>Image Preview</th>");
        html.Append("<th>Approve \ Reject</th>");
        html.Append("</tr>");
        html.Append("</thead>");
        html.Append("<tbody>");
        foreach (DataRow row in dt.Rows)
        {
            html.Append("<tr style='color:RoyalBlue'>");
            foreach (DataColumn column in dt.Columns)
            {
                html.Append("<td>");
                html.Append(row[column.ColumnName]);
                html.Append("</td>");
            }
            string path = row["IDCardUpload"].ToString();
            html.Append("<td>");
            Image img = new Image();
            img.ImageUrl = path;
            img.Width = 100;
            img.Height = 100;
            PlaceHolder2.Controls.Add(img);
            html.Append("</td>");  
            html.Append("<td align='center'>");
            html.Append("<button type='button' class='btn-success btn-s'>Approve</button> &nbsp; &nbsp; &nbsp;");
            html.Append("<button type='button' class='btn-danger btn-sm'>Reject</button>");
            html.Append("</td>");
            html.Append("</tr>");
        }
        html.Append("</tbody>");
        html.Append("</table>");
        PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });
        html.Append("</div>");
        html.Append("</div>");
    }
}
private DataTable GetData()
{
    string constr = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT FirstName,Surname,BirthdayDay,BirthdayMonth,BirthdayYear,Gender,IDCardUpload FROM Registration"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
}

我想在图像预览栏下显示图像缩略图。请帮忙。

使用这个代码,我的输出就像这个

输出

在您的代码中,您在HTML中有一些特定控件,而其他控件则使用占位符或.net类添加。可以使用System.Web.UI.WebControls命名空间中的TableCell、TableRow和其他类来针对类编写代码。无论哪种方式,使用所有HTML或使用所有类都需要保持一致。

但是,如果您需要使用内联html,那么您也可以在表单元格中使用图像html标记(Img)。

<img src="yourimagePath.gif" height="50px" width="50px"> 

在这里,您可以从Database值中动态添加src,并将其添加到td元素中。

最新更新