イベントを使用RowDataBound
して、GridView 行内の何かを変更できます。
protected void GridView1_RowDatabound(object sender, GridViewRowEventArgs e)
{
// check if this row is a row with Data (not Header or Footer)
if (e.Row.RowType == DataControlRowType.DataRow)
{
// find your Image control in the Row
Image image = ((Image)e.Row.FindControl("img"));
// set the path of image
img.ImageUrl = "path of image";
}
}
または、ステートメントを使用foreach
して Rows コレクションをループし、画像のパスを変更することもできます。このコードは Page_Load イベントにある可能性があります。
foreach(GridViewRow row in GridView1.Rows)
{
// check if this row is a row with Data (not Header or Footer)
if (row.RowType == DataControlRowType.DataRow)
{
// find your Image control in the Row
Image image = ((Image)row.FindControl("img"));
// set the path of image
img.ImageUrl = "path of image";
}
}
編集
ソリューションから画像を設定することはできません。これを行うには、ハンドラー (.ashx ファイル) を作成し、C: でイメージの名前を渡し、次のようなバイト [] を返す必要があります。
public void ProcessRequest(HttpContext context)
{
byte[] imageBytes = File.ReadAllBytes(@"C:\" + context.Request["image"]);
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(imageBytes);
}
そしてあなたのページで、image
URLにパラメータを渡すハンドラーによってそれを設定します:
Image image = ((Image)row.FindControl("img"));
img.ImageUrl = "ImageHandler.ashx?image=1.png";