如何在asp.net gridview中动态显示图像? [英] How to dynamically show images in a asp.net gridview?

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

问题描述

我有一个gridview,它有一列当前返回一个1,如果没有signoff则返回0,否则返回0。如果每行的值为1,或者如果为0,则我想显示一个竖起大拇指的图片。

I have a gridview that has a column that currently returns a "1" if there was a signoff, or 0 if not. I would like to show a "Thumbs up" image if the value is 1 for each row, or nothing if 0.

这样做的最佳方法是什么?我在想使用rowdatabound事件,最好的办法是什么?

What is the best method for doing this? I was thinking of somehow using the rowdatabound event, what is the best way to do this?

谢谢,

Mark。

推荐答案

rowdatabound事件确实为您提供了执行此操作的功能。这样做相当简单直观。如标记所述,您可以使用模板列。我可能只是使用模板列或图像字段来管理它:

The rowdatabound event certainly provides you with the ability to do this. It's fairly straight forward and intuitive to do so. And as mark mentioned, you could use a template column. I'd probably just use either the template column, or an image field to manage this:

<Columns>
    <asp:ImageField HeaderText='Sign Off' 
        DataImageUrlField='<%# ThumbDisplay(Eval("SignOff")) %>' />
</Columns>

然后,您需要在代码隐藏中使用类似以下的方法:

You'd then need a method like the following in your codebehind:

protected string ThumbDisplay(int signoff)
{
    return (signoff == 1) ? "~\thumbsup.png" : "~\thumbsdown.png";
}

http://www.asp.net/learn/data-access/tutorial-12-cs.aspx 有很好的细节关于使用模板列。

http://www.asp.net/learn/data-access/tutorial-12-cs.aspx has good detail on using the template column.

最好的方法可能是适合您其他显示需求的方法。其他专栏是否需要类似的按摩?在rowdatabound赛事中,你可能会更好地关注一切。但是,如果它只是对数据的唯一修改,那么使用模板列或图像列可能是最容易遵循的,并保持整个页面清洁。

The best method is probably the one that fits with your other display needs. Will other columns need similar massaging? You might be better off taking care of it all in one go in the rowdatabound event. But if it's going to be your only modification to the data, using a template column, or the image column, will probably be the easiest to follow and keep the whole page clean.

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

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