You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

未加runat="Server"标签,如何在GridView RowDataBound事件中查找HTML按钮

问题:未添加runat="Server"时,如何在GridView的RowDataBound事件中找到HTML输入按钮?

场景代码

GridView的HTML代码

<asp:GridView ID="grdFiles" runat="server" AutoGenerateColumns="false" Width="100%" OnRowDataBound="grdFiles_RowDataBound"    >
    <Columns>
        <asp:BoundField DataField="BatchNo" HeaderText="Batch No" />
        <asp:BoundField DataField="totalspnumber" HeaderText="Total SP Number" />
        <asp:BoundField DataField="VendorName" HeaderText="Vendor Name" />
        <asp:BoundField DataField="Location" HeaderText="Location" />
        <asp:BoundField DataField="prefix" HeaderText="Prefix" />
        <asp:BoundField DataField="dateofexport" HeaderText="Date Of Export" />
        <asp:BoundField DataField="ExpiryDate" HeaderText="Expiry Date" />

        <asp:TemplateField>
            <ItemTemplate>
                <input id="downloadButton" fileid="<%# Eval("id") %>" filename="<%# Eval("exportedFileName") %>" type="button" tee="<%# Eval("isexported") %>" value="<%# Eval("isexported").ToString()=="2"?"Download again?":"Download" %>" onclick="SetFileUploaded(this)" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

后台RowDataBound代码(当前返回null)

protected void grdFiles_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataControlFieldCell cell = (DataControlFieldCell)e.Row.Controls[7];

        // Access the button
        HtmlInputButton downloadButton = (HtmlInputButton)cell.FindControl("downloadButton");

        // Check the condition
        if (true)
        {
            // Hide the button
            downloadButton.Visible = false;
        }
    }
}

解决方案

未添加runat="server"的HTML元素不属于服务器端控件,后台无法直接通过FindControl方法获取。可以通过以下几种方式实现需求:

1. 给按钮添加runat="server"(最简便方案)

虽然你要求不添加,但这是最直接可靠的方式,修改按钮代码如下:

<input id="downloadButton" runat="server" ClientIDMode="Static" fileid="<%# Eval("id") %>" filename="<%# Eval("exportedFileName") %>" type="button" tee="<%# Eval("isexported") %>" value="<%# Eval("isexported").ToString()=="2"?"Download again?":"Download" %>" onclick="SetFileUploaded(this)" />

之后后台可以直接通过e.Row.FindControl("downloadButton")获取按钮对象,原代码修改后即可正常工作。

2. 后台修改单元格HTML内容控制按钮

如果必须保留按钮无runat="server"的状态,可以在RowDataBound中修改对应单元格的HTML,实现隐藏按钮的效果:

protected void grdFiles_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // 替换为你的实际判断条件
        bool needHideButton = true;

        if (needHideButton)
        {
            // 获取TemplateField对应的单元格(索引7,对应第8列)
            DataControlFieldCell targetCell = (DataControlFieldCell)e.Row.Controls[7];
            
            // 方式1:直接清空单元格内容
            targetCell.Text = string.Empty;

            // 方式2:给按钮添加隐藏样式(保留按钮DOM但不可见)
            // targetCell.Text = targetCell.Text.Replace("<input ", "<input style='display: none;' ");
        }
    }
}

注意:这种方式依赖单元格的HTML结构,若前端模板变更需同步调整代码。

3. 通过客户端脚本控制按钮显示

在后台根据条件输出客户端脚本,页面加载后自动隐藏目标按钮:

protected void grdFiles_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        bool needHideButton = true; // 替换为你的判断条件
        if (needHideButton)
        {
            // 获取当前行的fileid属性值,作为按钮的唯一标识
            string fileId = DataBinder.Eval(e.Row.DataItem, "id").ToString();
            
            // 注册客户端脚本,页面加载后隐藏对应按钮
            ClientScript.RegisterStartupScript(this.GetType(), $"hideBtn_{fileId}",
                $"document.querySelector('input[fileid=\"{fileId}\"]').style.display = 'none';", true);
        }
    }
}

这种方式通过客户端DOM操作实现,不依赖服务器控件,适合无法修改前端控件属性的场景。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 02:35:19