数据网格中的输出图像

发布于 2024-10-04 13:11:39 字数 155 浏览 0 评论 0原文

如何为绑定的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东京女 2024-10-11 13:11:39

首先,Eval 是邪恶的。 eval 有一个解决方案,但我会故意跳过它。

还有另外 2 个解决方案。您可以使用 ImageField 并调整数据,以在应在 ImageField 的 DataImageUrlField 属性中指定的特殊属性中传递图片的正确 URL。

另一个解决方案是添加带有 Image 控件的模板字段并处理 RowDataBound 事件,从事件参数 e.Row.DataItem 获取行的数据,将其转换为数据源中项目的类型并提取布尔值。然后您可以使用 FindControl 获取 Image 控件并分配其属性。像这样的事情:

protected void Page_Load(object sender, EventArgs e)
{
    gvTest.DataSource = new bool[] { true, false };
    gvTest.DataBind();
}

protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Image imgBool = (Image)e.Row.FindControl("imgBool");
        bool data = (bool)e.Row.DataItem;
        if (data)
        {
            imgBool.ImageUrl = "/images/true.jpg";
            imgBool.AlternateText = "This is true";
        }
        else
        {
            imgBool.ImageUrl = "/images/false.jpg";
            imgBool.AlternateText = "This is false";
        }
    }
}

这可能看起来需要大量工作,但请记住,您可以使用相同的处理程序以类型安全的方式处理网格中的所有列。

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:

protected void Page_Load(object sender, EventArgs e)
{
    gvTest.DataSource = new bool[] { true, false };
    gvTest.DataBind();
}

protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Image imgBool = (Image)e.Row.FindControl("imgBool");
        bool data = (bool)e.Row.DataItem;
        if (data)
        {
            imgBool.ImageUrl = "/images/true.jpg";
            imgBool.AlternateText = "This is true";
        }
        else
        {
            imgBool.ImageUrl = "/images/false.jpg";
            imgBool.AlternateText = "This is false";
        }
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文