我的代码中存在问题。我正在尝试使用C#,Asp.net技术在GridView
内添加图像。我只从数据库中检索图像名称,并希望将其与存储的文件夹路径一起添加。
faq.aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table class="table table-striped table-bordered margin-top-zero" >
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-4 col-sm-4">
<col class="col-md-4 col-sm-4">
<col class="col-md-2 col-sm-2">
<col class="col-md-1 col-sm-1">
</colgroup>
<thead>
<tr>
<th>Sl. No</th>
<th>Question</th>
<th>Answer</th>
<th>Image</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td><asp:Label ID="faqid" runat="server" Text='<%#Eval("FAQ_ID") %>'></asp:Label></td>
<td><asp:Label ID="question" runat="server" Text='<%#Eval("Question") %>'></asp:Label></td>
<td><asp:Label ID="answer" runat="server" Text='<%#Eval("Answer") %>'></asp:Label></td>
<td><asp:Image ID="Image1" runat="server" border="0" name="bannerimage" style="width:70px; height:70px;" ImageUrl='C:\ASP.NET\ODIYA_Doctor_Admin\ODIYA_Doctor_Admin\Upload\<%#Eval("Image")%>' /> </td>
<td><a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-success" data-original-title="Edit"><i class="fa fa-edit"></i></a>
<a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-danger" data-original-title="Delete"><i class="fa fa-times"></i></td>
</tr>
</tbody>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
faq.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
faqBL objFaqBL = new faqBL();
GridView1.DataSource = objFaqBL.getFaqData();
GridView1.DataBind();
}
我在视图中获取除图像之外的所有其他数据。图像也是我表格中的字段名称。请帮我解决这个问题。
答案 0 :(得分:0)
假设您将图像存储在SQL Server数据库中, 你需要创建Generic handler image.ashx或image.aspx,如下所示: 1.创建image.aspx页面 将此代码添加到Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] != null)
{
string sql1 = "SELECT Image FROM" +
" YourTableName WHERE faqid=" + Convert.ToInt32(Request.QueryString["id"].ToString());
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "your connection string";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (dr[0] != DBNull.Value)
{
Byte[] bytes = (Byte[])dr[0];
}
else
{
byte[] bytes = ReadFile(Server.MapPath("~/images/empty.PNG"));
Response.Buffer = true;
}
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
dr.Close();
conn.Close();
}
}
byte[] ReadFile(string sPath)
{
byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;
}
要在GridView中查看图像,请添加模板列,如下所示:
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<div style="text-align:center;">
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl='<%# "image.aspx?id="+ Eval("faqid") %>' Width="100px"
Height="70px" Style="cursor: pointer" />
</div>
</ItemTemplate>
<ItemStyle Width="300px" HorizontalAlign="Left" />
</asp:TemplateField>
希望有所帮助。
答案 1 :(得分:0)
正如Ondrej所说,ImageUrl使用URL,例如example.com/Upload/myimage.png。假设您的上传文件夹位于Web应用程序的根目录中,如果您将ImageUrl更改为
,它将起作用ImageUrl='<%# "Upload/" + Eval("Image")%>'
试一试:)