You need to enable JavaScript to run this app. ASP.NET WebForms中如何按行删除状态修改GridView内HyperLink的ImageUrl
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET WebForms中如何按行删除状态修改GridView内HyperLink的ImageUrl

解决方案

你只需要先给TemplateField里的HyperLink控件设置唯一ID,再在RowDataBound事件中通过FindControl定位到该控件修改属性即可,具体实现如下:

步骤1:修改前端GridView代码,给HyperLink加ID

<asp:GridView ID="gvReport" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="true" OnRowDataBound="gvReport_RowDataBound" CssClass="table table-bordered">
  <Columns>
    <asp:BoundField DataField="Delete" HeaderText="Delete" SortExpression="Delete" />
    <asp:TemplateField HeaderText="Delete">
      <ItemTemplate>
        <!-- 新增ID属性,值可自定义 -->
        <asp:HyperLink ID="hlDeleteBtn" runat="server" NavigateUrl='<%# "Edit?from=Delete&ID=" + Eval("ID") %>' ImageUrl="Assets/SmallDelete.png" SortExpression="PathToFile" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

步骤2:修改后端RowDataBound事件逻辑

if (e.Row.RowType == DataControlRowType.DataRow)
{
    TableCell statusCell = e.Row.Cells[0];
    if (statusCell.Text == "True")
    {
        // 查找当前行内的HyperLink控件
        HyperLink deleteLink = e.Row.FindControl("hlDeleteBtn") as HyperLink;
        if (deleteLink != null)
        {
            // 替换为你的蓝色图标实际存储路径
            deleteLink.ImageUrl = "Assets/SmallDeleteBlue.png";
        }
    }
}

注:不建议通过单元格索引+控件索引定位HyperLink,后续如果调整列顺序会导致逻辑失效,使用ID查找的方式兼容性更高

内容的提问来源于stack exchange,提问作者brandoonjen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 11:00:03