GridView OnRowData Bound, Img_Data from SQL Base64string wit



在我的页面上,我有一个Gridview字段超过SQl自动没有模板字段。所有设置都在代码后面。ondatabound

现在我有一个Base64字符串在sql表和gridview不显示图像。我在结果中看到base64string。

如何将字符串转换为图像后面的代码节中没有TemplateFilds。模板字段是不能够Gridview是动态结束数据库的多重组合。只有前两列是静态的。

ASPX

<asp:GridView ID="mergeresult" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan" OnRowCreated="mergeresult_OnRowCreated"
OnRowDataBound="mergeresult_OnRowDataBound" BorderWidth="1px" CellPadding="2" 
ForeColor="Black" GridLines="Both" autogeneratedcolumns=false>
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
<HeaderStyle Font-Size="Small" Font-Underline="true" />
<RowStyle Font-Size="12px" />
</asp:GridView>

c#

protected void mergeresult_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
int totalRow = mergeresult.Rows.Count;
int totalColumns = mergeresult.Columns.Count;

for (int i = 0; i < totalRow + 1; i++)
{
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
// Image Converting ?? Field e.Row.Cells[1] have the base64 string inside.

}

}
}





}

DataBind

protected void run_Click(object sender, EventArgs e)
{ 
// Dynamicel results from SQL Databases

mergeresult.DataSource = dt1;
mergeresult.DataBind();
}

操作SQL,使输出如下所示,如何在HTML中显示Base64图像

<div>
<p>Taken from wikpedia</p>
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
</div>

感谢@JobesK的解决方案。我用下面的代码解决了我的问题:

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text.Length >= 500)
{
// string data = "";
String TagStart1 = "";
String Coding = "";
String TagEnd = "";
string Combi = "";
String Dimension = "";
string AltText = "";
//data = cell.Text.ToString();
TagStart1 = "<img src=";
Coding = "data:image/jpg;base64,";
TagEnd = """;
Combi = TagStart1 + TagEnd + Coding + e.Row.Cells[1].Text + TagEnd;
Dimension = " width="65px" height="65px">";
AltText = " alt="testing""; // "alt="" + e.Row.Cells[4].ToString() +""";
e.Row.Cells[1].Text = Combi + AltText + Dimension;
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
}
else
{
e.Row.Cells[1].Text = "Kein Bild";
}
}

你知道如何把原始格式的图像从base64放入<span high="75px" width="75px">标签的格式调整大小吗?

最新更新