如何在文件夹中显示ASP net gridview中的图像 [英] How to display images in ASP net gridview from folder

查看:60
本文介绍了如何在文件夹中显示ASP net gridview中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想根据行值显示webserver文件夹中的图像。

数据库很简单,只有两列(字符串用户名,int状态)我们将获得基于WPF应用程序的状态,并且有两种状态:0,1

它有效,这很好,但我想显示通过公司的网络应用程序平台获得结果。



我从一个简单的GridView开始,为图像添加了一个额外的列:



 <   asp:GridView     ID   =  Grid1    PageSize   =  50    DataSourceID   =  LinqStateSource    AllowPaging   =  True < span class =code-attribute>   OnRowDataBound   =  GridView1_RowDataBound  ShowFooter     =   True    

AllowSorting = True runat = < span class =code-keyword> server AutoGenerateColumns = 错误 >
< >
< asp:BoundField DataField = 用户名 HeaderText = 用户名 SortExpression = 用户名 / >
< asp:BoundField DataField = HeaderText = 状态 SortExpression = / >
< asp:ImageField HeaderText = 图片 / >
< /列 < span class =code-keyword>>
< / asp:GridView >

<! - - LINQ! --->!;
< asp:LinqDataSource ID =LinqStateSourcerunat =serverContextTypeName =... WebApp.data.LinqStateSourceDataContextEntityTypeName =TableName =tblname/>





它到目前为止有效,但现在我要显示一张图片(绿色,红色圆点)而不是状态列。



在Code背后我开始说:



 < span class =code-keyword> protected   void  GridView1_RowDataBound( object  sender,GridViewRowEventArgs e )
{

foreach (GridViewRow row in Grid1.Rows )
{
for int i = 0 ; i < Grid1.Columns.Count; i ++)
{
/ * if(e.Row.Cells [1] .Text ==1)
e.Row .Cells [2] .Image = @... \ images \greendot.ico;
| _______mindcode
else
e.Row.Cells [2] .Image = @... \ images \ reddot.ico;
| _______mindcode
/ *
}
}

}





我不知道怎么管理它。



谢谢你,

Vincent



我尝试了什么:



  protected   void  GridView1_RowDataBound( object  sender,GridViewRowEventArgs e)
{

foreach (GridViewRow row in Grid1.Rows)
{
for int i = 0 ; i < Grid1.Columns.Count; i ++)
{
/ * if(e.Row.Cells [1] .Text ==1)
e.Row.Cells [2] .Image = @... \ images \greendot.ico ;
| _______mindcode
else
e.Row.Cells [2] .Image = @... \ images \ reddot.ico;
| _______mindcode
/ *
}
}

}

解决方案

我发现更容易做到以下选项之一:



选项1:绑定标记:

1.准备数据源以在查询中包含url(可能在数据库查询中放置一些case when样式逻辑)。例如SELECT url AS imageUrl FROM myTable ...

2.将图像源URL绑定到标记上控制

例如

< img src = <%#Eval(imageUrl)/>



选项2:在行数据绑定上添加图像控件

  string  imageUrlValue =  @ 。 ..\images\ {0} .ICO; 
DataRow myRow = e.Row.DataItem as DataRow;
Image img = new Image();
img.ImageUrl =
string .Format

imageUrlValue,
myRow [ Mycolumn] == 1 greendot reddot
);
row.Cells [ 2 ]。Controls.Add(img);


另一种选择:使用代码隐藏函数将图像绑定到标记中,以生成URL:

 <   asp:Image     runat   =  server  

ImageUrl = ' <%#BuildStateImageUrl(( int )Eval( State))%> '

/ >



  protected   string  BuildStateImageUrl( int  state)
{
switch (state)
{
case 1
{
return 〜/ images / greendot.png;
}
默认
{
返回 < span class =code-string> 〜/ images / reddot.png;
}
}
}



NB:图标文件( .ico )不保证在所有浏览器中都有效。您应该将文件转换为标准Web格式之一( .png .jpg .gif注意)。


Hello everyone,

I want to display an image from webserver's folder based on row values.
The database is pretty easy, just two columns (string username, int state) we'll get the state based on a WPF application and there are two states: 0,1
It works, that is nice but I want to display the results via the company's web-application platform.

I started with a simple GridView and added an extra column for the image:

        <asp:GridView ID="Grid1" PageSize="50" DataSourceID="LinqStateSource" AllowPaging="True" OnRowDataBound="GridView1_RowDataBound"ShowFooter = "True" 

 AllowSorting="True" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="username" HeaderText="Username" SortExpression="username" />
        <asp:BoundField DataField="state" HeaderText="State" SortExpression="State" />
        <asp:ImageField HeaderText="Picture" />
    </Columns>
</asp:GridView>

<!--- LINQ! ---!>
<asp:LinqDataSource ID="LinqStateSource" runat="server" ContextTypeName="...WebApp.data.LinqStateSourceDataContext" EntityTypeName="" TableName="tblname"/>



It works so far, but now I want to show a picture (green, red dots) instead of the "state" column.

In Code behind I started with that:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {

           foreach (GridViewRow row in Grid1.Rows)
           {
               for (int i = 0; i < Grid1.Columns.Count; i++)
               {
                   /*if(e.Row.Cells[1].Text== "1")
                       e.Row.Cells[2].Image = @"...\images\greendot.ico";
                        |_______ "mindcode"
                   else
                       e.Row.Cells[2].Image = @"...\images\reddot.ico";
                        |_______ "mindcode"
                      /*
               }
           }

       }



I have no clue how to manage it.

Thank you,
Vincent

What I have tried:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {

           foreach (GridViewRow row in Grid1.Rows)
           {
               for (int i = 0; i < Grid1.Columns.Count; i++)
               {
                   /*if(e.Row.Cells[1].Text== "1")
                       e.Row.Cells[2].Image = @"...\images\greendot.ico";
                        |_______ "mindcode"
                   else
                       e.Row.Cells[2].Image = @"...\images\reddot.ico";
                        |_______ "mindcode"
                      /*
               }
           }

       }

解决方案

I find it easier to do one of the following options:

OPTION 1: bind in markup:
1. Prepare your datasource to include the url on query (maybe put some "case when " style logic in your DB query). e.g. SELECT url AS imageUrl FROM myTable...
2. Bind image source url to control on markup
e.g.
<img src="<%#Eval("imageUrl")" />

OPTION 2: Add image control on row data bind

string imageUrlValue = @"...\images\{0}.ico";
            DataRow myRow = e.Row.DataItem as DataRow;
            Image img = new Image();
            img.ImageUrl = 
                string.Format
                (
                    imageUrlValue,
                    myRow["Mycolumn"] == "1" ? "greendot" : "reddot"
                );
            row.Cells[2].Controls.Add(img);


Another option: bind the image in markup, using a code-behind function to generate the URL:

<asp:Image runat="server"

    ImageUrl='<%# BuildStateImageUrl((int)Eval("State")) %>'

/>


protected string BuildStateImageUrl(int state)
{
    switch (state)
    {
        case 1:
        {
            return "~/images/greendot.png";
        }
        default:
        {
            return "~/images/reddot.png";
        }
    }
}


NB: Icon files (.ico) are not guaranteed to work in all browsers. You should convert the files to one of the standard web formats instead (.png, .jpg or .gif).


这篇关于如何在文件夹中显示ASP net gridview中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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