【问题标题】:Image on image button not displaying when changing it dynamically based on the value of a cell in Gridview根据Gridview中单元格的值动态更改图像按钮时,图像按钮不显示
【发布时间】:2018-01-04 07:28:44
【问题描述】:

我正在尝试根据 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 ";

            }

        }

    }

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

【问题讨论】:

    标签: asp.net gridview imagebutton


    【解决方案1】:
    1. 在 aspx 文件中删除 ImageUrl=''。
    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 ";
      

    【讨论】:

    • 我删除了 ImageUrl ..就像你说的那样,我用你建议的代码替换了代码:protected void grd_UserDetails_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType. DataRow) { ImageButton img = (ImageButton)e.Row.FindControl("img_user"); string status = (string)DataBinder.Eval(e.Row.DataItem, "Status"); img.ImageUrl = status == "活动" ? "~/images/active.png" : "~/images/inactive.png "; } }
    • 但是图像仍然没有显示出来。
    • 你的代码和我的代码都应该是正确的。您是否检查了 root\images\ 中的图像?你可以尝试像“~\images\active-inactive.png”这样的斜杠到反斜杠吗?
    • 是的..图像在根文件夹中。我也试过 \\.还是不行。
    • 是的..检查问题中的图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2018-04-19
    • 1970-01-01
    相关资源
    最近更新 更多