数据网格中的输出图像
如何为绑定的 gridview 进行自定义输出?即我在数据库中有一个布尔字段,而不是像我从 <%# DataBinder.Eval(Container.DataItem, "BoolField") %> 得到的那样写出 True ,我想放置一个图像(例如网格视图中的勾号)。这可能吗?
How can you do custom output for a bound gridview? I.e. I have a boolean field in a database, rather than write out True as I get from <%# DataBinder.Eval(Container.DataItem, "BoolField") %>, I'd like to put an image (such as a tick) in the grid view. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,Eval 是邪恶的。 eval 有一个解决方案,但我会故意跳过它。
还有另外 2 个解决方案。您可以使用 ImageField 并调整数据,以在应在 ImageField 的 DataImageUrlField 属性中指定的特殊属性中传递图片的正确 URL。
另一个解决方案是添加带有 Image 控件的模板字段并处理 RowDataBound 事件,从事件参数 e.Row.DataItem 获取行的数据,将其转换为数据源中项目的类型并提取布尔值。然后您可以使用 FindControl 获取 Image 控件并分配其属性。像这样的事情:
这可能看起来需要大量工作,但请记住,您可以使用相同的处理程序以类型安全的方式处理网格中的所有列。
First of all Eval is Evil. There is a solution with eval but I will skip it on purpose.
There are 2 other solutions. You can use the ImageField and shape the data to pass the corret URL for the picture in a special property that you should specify in the DataImageUrlField property of the ImageField.
The other solution is to add a template field with an Image control and handle the RowDataBound event get the data for the row from the event arguments e.Row.DataItem cast it to the type of the items in your data source and extract the boolean value. Then you can use FindControl to get the Image control and assing its properties. Something like this:
This might seem like a lot of work but remember that you can use this same handler to work with all the colums in your grid in a type safe way.
在 GridView 列中显示图像
Displaying Images in a GridView Column