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

ASP.NET GridView中New命令无响应问题排查与解决

GridView点击New无响应的问题修复方案

核心问题

GridView本身没有内置插入行的UI,ShowInsertButton仅生成"New"链接,但点击后不会自动显示插入空行;同时你的主键字段设置和InsertCommand存在冲突,导致插入逻辑无法执行。

具体修复步骤

1. 修正SqlDataSource的插入配置

如果id是数据库自增字段,必须移除InsertCommand中的id字段和参数——数据库会自动生成该值,否则插入时因无法提供id值导致操作失败:

<!-- 修改后的InsertCommand -->
InsertCommand="INSERT INTO [deal_master] ([agreement_no], [status], [sales_person], [swap_carrier], [start_pos], [end_pos], [swap_commitment], [zone], [target_minutes], [target_sell_rate], [target_buy_rate], [supplier_interconnect], [customer_interconnect], [target_sales], [target_cost], [target_profit], [lcr_zone]) VALUES (@agreement_no, @status, @sales_person, @swap_carrier, @start_pos, @end_pos, @swap_commitment, @zone, @target_minutes, @target_sell_rate, @target_buy_rate, @supplier_interconnect, @customer_interconnect, @target_sales, @target_cost, @target_profit, @lcr_zone)"

同时删除InsertParameters里的<asp:Parameter Name="id" Type="Int32" />节点。

另外,agreement_no当前设为只读,插入时无法输入值,需将其改为可编辑(如果插入时需要手动输入),或通过代码自动赋值。

2. 改用FooterTemplate实现插入行

GridView的标准插入方式是用FooterTemplate作为插入区域,步骤如下:

  1. 移除CommandField中的ShowInsertButton="True";
  2. 开启GridView的ShowFooter="True";
  3. 给每个需要插入的字段添加FooterTemplate,放入对应的输入控件;
  4. 在Footer中添加"保存"和"取消"按钮;
  5. 处理RowCommand事件执行插入逻辑。

示例代码片段:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="agreement_no,id" Font-Size="9pt" DataSourceID="SqlDataSource1" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" OnRowCreated="GridView1_RowCreated" OnRowCommand="GridView1_RowCommand" ShowFooter="True">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"></asp:CommandField>
        <!-- 把agreement_no的BoundField改为TemplateField,添加Footer输入框 -->
        <asp:TemplateField HeaderText="agreement_no" SortExpression="agreement_no">
            <ItemTemplate>
                <asp:Label runat="server" Text='<%# Bind("agreement_no") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label runat="server" Text='<%# Bind("agreement_no") %>'></asp:Label>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtNewAgreementNo" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <!-- status字段添加Footer下拉框 -->
        <asp:TemplateField HeaderText="status" SortExpression="status">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="status" DataValueField="status" SelectedValue='<%# Bind("status") %>'>
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:GPGConnectionString %>" SelectCommand="SELECT DISTINCT [status] FROM [deal_master]"></asp:SqlDataSource>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label runat="server" Text='<%# Bind("status") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="ddlNewStatus" runat="server" DataSourceID="SqlDataSource2" DataTextField="status" DataValueField="status"></asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>
        <!-- 其他字段同理添加FooterTemplate -->
        <asp:TemplateField HeaderText="操作">
            <FooterTemplate>
                <asp:Button ID="btnSave" runat="server" Text="保存" CommandName="InsertNew" />
                <asp:Button ID="btnCancel" runat="server" Text="取消" CommandName="CancelInsert" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

3. 编写后台插入逻辑

在后台代码中处理RowCommand事件:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "InsertNew")
    {
        // 获取Footer中的输入控件值
        TextBox txtAgreementNo = GridView1.FooterRow.FindControl("txtNewAgreementNo") as TextBox;
        DropDownList ddlStatus = GridView1.FooterRow.FindControl("ddlNewStatus") as DropDownList;
        // 其他字段同理获取

        // 给SqlDataSource的插入参数赋值
        SqlDataSource1.InsertParameters["agreement_no"].DefaultValue = txtAgreementNo.Text;
        SqlDataSource1.InsertParameters["status"].DefaultValue = ddlStatus.SelectedValue;
        // 其他参数依次赋值

        // 执行插入并刷新GridView
        SqlDataSource1.Insert();
        GridView1.DataBind();
    }
    else if (e.CommandName == "CancelInsert")
    {
        // 取消插入,刷新页面清空输入
        GridView1.DataBind();
    }
}

4. 检查回发是否正常

打开浏览器控制台,确认没有JavaScript错误;如果页面使用了UpdatePanel,确保GridView在UpdatePanel内部,或配置了正确的回发触发器。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 23:00:54