我正在创建一个表,它应该从DB读取二进制图像并显示在Html表中。
我的标记:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Table Test</title>
<style type="text/css">
.small-ad-wrapper{width:215px;height:195px;overflow:hidden;}
.small-ad-wrapper .portfolio-img-control{width:204px;height:134px;overflow:hidden;margin-bottom:3px;}
</style>
</head>
<body>
<form id="Form1" runat="server">
<div>
<br />
<asp:Button ID="cmdCreate" OnClick="cmdCreate_Click" runat="server" Text="Create" />
<br />
<br />
<asp:Table ID="tbl" runat="server" />
</div>
</form>
</body>
</html>
我的守则背后:
StringBuilder htmlTable = new StringBuilder();
htmlTable.Append("<table border='1'>");
htmlTable.Append(@"<tr style='background-color:green; color: White;'>
<th>Company Name</th>
</tr>");
int i = 1;
foreach (DataRow row in DT.Rows)
{
if (i == 1)
{
htmlTable.Append("<tr style='color: White;'>");
}
htmlTable.Append("<div class='portfolio-img-control'>");
byte[] bytes = (byte[])(byte[])row["LogoFile"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
System.Web.UI.WebControls.Image imgNew = new System.Web.UI.WebControls.Image();
imgNew.ImageUrl = "data:image/png;base64," + base64String;
htmlTable.Append("<td>" + imgNew + "</td>");
htmlTable.Append("</div");
if (i == 3)
{
htmlTable.Append("</tr>");
i = 1;
}
else
i = i + 1;
}
htmlTable.Append("</table>");
DBDataPlaceHolder.Controls.Add(new Literal { Text = htmlTable.ToString() });
htmlTable.Append("<tr>");
htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");
htmlTable.Append("</tr>");
但它给了我结果&#34; System.Web.UI.WebControls.Image&#34;并没有显示图像。
怎么做?请回复谢谢
答案 0 :(得分:0)
因为您创建了一个新的asp.net图像控件,并尝试将该对象用作字符串。
必须删除这些行......
System.Web.UI.WebControls.Image imgNew = new System.Web.UI.WebControls.Image();
imgNew.ImageUrl = "data:image/png;base64," + base64String;
htmlTable.Append("<td>" + imgNew + "</td>");
如果您想将其渲染为字符串,则必须更改为:
htmlTable.AppendFormat("<td><img src=\"data:image/png;base64,{0}\"></td>", base64String);
请注意,我不知道你的其余代码是否没有错误......