【问题标题】:Replace boolean with an image in a ASPXGridView用 ASPXGridView 中的图像替换布尔值
【发布时间】:2014-02-01 04:49:25
【问题描述】:

我的 ASPXGridView 上有几列包含布尔值。

我想用勾号图标替换“真”的值。 然后我想用十字图标替换错误的值。

有没有办法做到这一点?我已经有了勾号和十字的图标图像。

谢谢

【问题讨论】:

    标签: c# asp.net devexpress boolean aspxgridview


    【解决方案1】:

    你可以这样做:

    ASPX:

    <ItemTemplate>
        <asp:Image ID="imgBooleanState" runat="server" AlternateText="State" ImageUrl='<%# GetBooleanState(Eval("MyBooleanValue")) %>' />
    </ItemTemplate>
    

    后面的代码:

    protected string GetBooleanState(object state)
    {
        bool result = false;
        Boolean.TryParse(state.ToString(), out result);
        if (result)
        {
             return ResolveUrl("~/path/tick.png");
        }
        else
        {
             return ResolveUrl("~/path/cross.png");
        }
    }
    

    【讨论】:

      【解决方案2】:

      您必须检查 rowdatabound 上的布尔值并相应地绑定图像,如下所示:

      protected void gridView_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
          Image imgCtrl = (Image) e.Row.FindControl("imgCtrl");
          if(e.Row.RowType == DataControlRowType.DataRow)
          {
              If(e.Row.DataItem("BooleanField"))
              {
              imgCtrl.ImageUrl = TickImagePath;
              }
              else
              {
              imgCtrl.ImageUrl = CrossImagePath;  
              } 
          }
      }
      

      在上面的代码中,我只是检查了rowdatabound 上的boolean 字段的值,并相应地设置了image path 或决定是设置“交叉图像路径”还是设置“勾选图像”路径。

      【讨论】:

        猜你喜欢
        • 2015-08-28
        • 2013-12-31
        • 1970-01-01
        • 2021-08-03
        • 2022-08-16
        • 1970-01-01
        • 1970-01-01
        • 2021-09-27
        • 1970-01-01
        相关资源
        最近更新 更多