根据 Gridview 中单元格的值动态更改图像按钮时不显示图像按钮

Image on image button not displaying when changing it dynamically based on the value of a cell in Gridview

我正在尝试根据 GridView 上单元格的值更改图像按钮上显示的图像。我尝试了以下方法:

<asp:GridView ID="grd_UserDetails" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                             GridLines="None" Width="600px" CellPadding="4" ForeColor="#333333" OnPageIndexChanging="grd_UserDetails_PageIndexChanging" OnSelectedIndexChanged="grd_UserDetails_SelectedIndexChanged">
                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            <Columns>
                                 <asp:BoundField DataField="userId" HeaderText="UserId" ><ItemStyle HorizontalAlign="Center" /></asp:BoundField>
                                   <asp:BoundField DataField="password" HeaderText="Password" ><ItemStyle HorizontalAlign="Center" /></asp:BoundField>
                                <asp:BoundField DataField="username" HeaderText="UserName" ><ItemStyle HorizontalAlign="Center" /></asp:BoundField>
                                <asp:BoundField DataField="email" HeaderText="Email Id" ></asp:BoundField>                                        


                                 <asp:BoundField DataField="status" HeaderText="Status" SortExpression="Active" />
                                <asp:TemplateField>

<ItemTemplate>

<asp:ImageButton ID="img_user" runat="server" CommandName="Select"

    ImageUrl='<%# Eval("status") %>' Width="20px" Height="20px" />

</ItemTemplate>

</asp:TemplateField>
                             </Columns>
                        </asp:GridView>

代码隐藏:

  protected void grd_UserDetails_RowDataBound(object sender, GridViewRowEventArgs e)

    {
        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            ImageButton img = (ImageButton)e.Row.FindControl("img_user");



            if (e.Row.Cells[4].Text == "Active")

            {

                img.ImageUrl = "~/images/active.png ";

            }

            else

            {

                img.ImageUrl = "~/images/inactive.png ";

            }

        }

    }

但是图像按钮不显示任何图像。我该如何解决这个问题。我的图像似乎正确放置在根目录中。

  1. 在 aspx 文件中删除 ImageUrl='<%# Eval("status") %>'。
  2. 在aspx.cs文件中,请尝试如下。 (我假设 gridview cell[4] 的数据字段名称是 "Status")。

    ImageButton img = (ImageButton)e.Row.FindControl("img_user");
    string status = (string)DataBinder.Eval(e.Row.DataItem, "Status");
    img.ImageUrl = status == "Active" ? "~/images/active.png" : "~/images/inactive.png ";