根据文本将图像插入到Gridview单元格中 [英] Insert an image into a Gridview cell depending on text

查看:64
本文介绍了根据文本将图像插入到Gridview单元格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS 2013(VB)和asp.net.

I use VS 2013 (VB) and asp.net.

我有一个使用MS SQL 2012表中的值以编程方式创建的gridview.

I have a gridview created programatically using values from an MS SQL 2012 table.

gridview的asp.net如下

The asp.net for the gridview is as follows

<asp:GridView ID="availableRuns" runat="server" HorizontalAlign="Center" CssClass="Asset_table" HeaderStyle-CssClass="Asset_table_header" ></asp:GridView>

gridview的VB代码是

The VB code for the gridview is

    Dim RTORun As New RTO
    Dim RTORuns As New List(Of RTO)
    RTORuns = RTORun.getRTO
    availableRuns.DataSource = RTORuns
    availableRuns.DataBind()

我要使用另一个子项将单元格中的值转换为图像,当前用于更改文本值.

I have another sub that I want to use to convert the values in the cells to images which is Currently used to change the text value.

    Protected Sub changeText()

    Dim row As GridViewRow

    For Each row In availableRuns.Rows
        Dim i As Integer

        For i = 0 To row.Cells.Count - 1
            If row.Cells(i).Text = "1" Then
                row.Cells(i).Text = "Yes"
            ElseIf row.Cells(i).Text = "0" Then
                row.Cells(i).Text = ""
            End If
        Next

    Next
End Sub

这将产生以下gridview

This produces the following gridview

我希望能够插入一个图像来代替1(或者是,因为它在图像中),如果为0,则将其留空.我不知道该怎么做,任何人都可以提供一些指针,因为谷歌搜索没有帮助因为它们都引用了我没有的asp.net页面中的图像控件.

I want to be able to insert an image in replacement for 1 (or Yes as it is in the image) and leave it blank if 0. I have no idea how to do this can anyone offer some pointers as googling hasnt helped as they all reference an image control in the asp.net page which I dont have.

推荐答案

您可以通过两种方式进行操作-直接分配HTML:

You can do it in 2 ways - either direct HTML assignment:

If row.Cells(i).Text = "1" Then
                row.Cells(i).Text = "<img src='MyYesImg.jpg' />"

其中"MyYesImg.jpg"是与ASPX页面位于同一文件夹中的图像.另外,您还必须确保网格列的HtmlEncode属性设置为False才能正确显示图像.

where "MyYesImg.jpg" is an image that exists in the same folder as your ASPX page. Also you have to make sure that grid columns have their HtmlEncode property set to False for image to propertly display.

或者,通过编程添加图像控件(您拥有 -它是标准的ASP.NET控件,是框架的一部分)

Or, by programmaticaly adding Image Control (which you do have - it's a standard ASP.NET control, part of the framework)

Dim img As Image
'...loop code
If row.Cells(i).Text = "1" Then
    row.Cells(i).Text = ""
    img = New Image
    img.ImageUrl = "MyYesImg.jpg"
    row.Cells(i).Controls.Add(img)

同样,"MyYesImg.jpg"是与ASPX页面位于同一文件夹中的图像文件

Again "MyYesImg.jpg" is the image file that exists in the same folder as your ASPX page

这篇关于根据文本将图像插入到Gridview单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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