从数据库将图像显示到gridview中 [英] Show images into gridview from database

查看:96
本文介绍了从数据库将图像显示到gridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用sql server作为后端,将图像名称存储为varchar。

我想检索这些图像并将它们显示到网格中查看。

我正在使用Linq来检索数据。

我使用了asp:图像控件到列itemtemplate。

但是我没有想法,怎么做。

我应该改变表结构吗? DataSource属性无法工作我想

请帮帮我..

谢谢

解决方案

绑定数据后数据表

然后

在网格视图中你可以像这样调用图像



< asp:image id = imgPicrunat =serverheight =50pxwidth =50pximageurl =<%#Eval(studentphotopath)=%& gt;=xmlns:asp =#unknown >






我假设您已将图像路径保存在数据库中。

请尝试以下代码。



在Aspx页面。



< pre lang =HTML> < asp:gridview id = grid runat = server onrowdatabound = grid_RowDataBound xmlns:asp = #unknown >
< >
< asp:templatefield >
< itemtemplate >
< asp:image runat = server id = img width = < span class =code-keyword> 100px height = 100px / >
< / itemtemplate >
< / asp:templatefield >
< / columns >
< / asp:gridview >




你的代码中的




< pre lang =c#> protected void grid_RowDataBound( Object sender,GridViewRowEventArgs args)
{
if (args.Row.RowType == DataControlRowType.DataRow)
{
DataRowView rowView =(DataRowView)args.Row.DataItem;
Image img =(Image)args.Row.FindControl( img);
img.ImageUrl = rowView [ Images]。ToString();
}
}





注意这比使用EVAL或绑定在aspx中更快。





希望这会有所帮助。


Hi,
I am using sql server as a back-end to store name of image as varchar.
I want to retrieve those images and show them into grid view.
I am using Linq to retrieve data.
I have used asp:Image control into column itemtemplate.
But I have no Idea, how to do that.
Should I change table structure? DataSource property wont work I guess
Please help me out..
Thanks

解决方案

after binding data in datatable
then
in grid view you can call image like this

<asp:image id="imgPic" runat="server" height="50px" width="50px" imageurl="<%#Eval(" studentphotopath")="" %&gt;"="" xmlns:asp="#unknown">


Hi,

I am assuming you have saved the image path in database.
Try following code.

in Aspx page.

<asp:gridview id="grid" runat="server" onrowdatabound="grid_RowDataBound" xmlns:asp="#unknown">
     <columns>
         <asp:templatefield>
             <itemtemplate>
                 <asp:image runat="server" id="img" width="100px" height="100px" />
             </itemtemplate>
         </asp:templatefield>
     </columns>
 </asp:gridview>



in your code behind

protected void grid_RowDataBound(Object sender, GridViewRowEventArgs args)
       {
           if (args.Row.RowType == DataControlRowType.DataRow)
           {
               DataRowView rowView = (DataRowView)args.Row.DataItem;
               Image img = (Image)args.Row.FindControl("img");
               img.ImageUrl = rowView["Images"].ToString();
           }
       }



Note this is faster than using EVAL or bind in your aspx.


Hope this helps.


这篇关于从数据库将图像显示到gridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆