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

ASP.NET中UpdatePanel内GridView SelectedIndexChanging事件不生效问题

问题描述

我将说明我的现有实现、预期需求以及遇到的问题:

现有实现

我有一个包含UpdatePanel的模态框(使用UpdatePanel是为了避免回发导致模态框关闭),模态框内有2个文本框、1个按钮和1个GridView。在文本框中输入人员的姓名和姓氏后,点击按钮查询数据并绑定到GridView。
GridView提供行选择控件,选中目标行后,我会取前三个单元格的内容更新隐藏域(HiddenField)的值,供后续其他函数调用。模态框底部的添加按钮默认处于禁用状态,需要选中行事件触发后启用该按钮,点击按钮会将选中数据填充到外部文本框中。

预期需求

选中所需GridView行后,启用添加按钮,点击按钮可将选中数据填充到指定文本框。

遇到的问题

选中GridView行时,SelectedIndexChanging事件会触发,但始终无法启用添加按钮,也无法将数据填充到目标文本框。测试时我曾去掉按钮的禁用属性,发现点击按钮时hfidNombreSocio的取值为空,且模态框不会关闭,怀疑是UpdatePanel使用方式有误。

前端代码

<div class="modal fade" id="modalSocio" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="staticBackdropLabelSocio">
                    <asp:Label ID="Label4" runat="server" CssClass="text-primary"></asp:Label>
                </h5>
               
            </div>
            
            
            <asp:UpdatePanel ID="UpdatePanel1"  runat="server" > 
          
                <ContentTemplate>
                    <div class="modal-body">
                        <div class="row g-3">
                                
                                
            <div class="row">
   <div class="col-3">
                <asp:TextBox ID="txtBusquedaNombre" runat="server" CssClass="form-control" placeholder="Nombre"></asp:TextBox>
       </div>
                <div class="col-3">
       <asp:TextBox ID="txtBusquedaApellido" runat="server" CssClass="form-control" placeholder="Apellido"></asp:TextBox>
            </div>
                <div class="col-3">
                <asp:LinkButton ID="btnBuscarSocio" runat="server"  CssClass="btn btn-outline-success" Text="Buscar" CausesValidation="false" ToolTip="Buscar" OnClick="btnBuscarSocio_Click" ><span class="fas fa-search"></span></asp:LinkButton>
             
            </div>
                  </div>
             
                            <div class="table-responsive mt-3">
            <asp:GridView ID="gvSocios" runat="server" CssClass="table table-bordered" AutoGenerateColumns="False"  CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanging="gvSocios_SelectedIndexChanging"    >
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField DataField="Legajo" HeaderText="Nro. Socio" ></asp:BoundField>
                    <asp:BoundField DataField="nombreSocio" HeaderText="Nombre" />
                    <asp:BoundField DataField="Apellido" HeaderText="Apellido" />
                    
                   
                    
                <asp:CommandField ButtonType="Link"  HeaderText="Seleccionar"  ShowSelectButton="True" SelectText="<i class='fa fa-check-circle'></i>">
                    <ControlStyle CssClass="btn btn-outline-secondary" />
                    </asp:CommandField>
           </Columns>
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />
            <EmptyDataTemplate>
                    <div class="alert alert-primary" role="alert">
                        No se encontraron registros!
                    </div>                        
                </EmptyDataTemplate>
            </asp:GridView>
                            </div>
                                   </div>
                      </div>
                      <!-- 把模态框底部按钮、隐藏域挪到ContentTemplate内部  -->
                      <div class="modal-footer">
                          <asp:HiddenField ID="hfSocio" runat="server" />
                          <asp:HiddenField ID="hfidNombreSocio" runat="server" />
                          <asp:Button ID="BtnCancelarSocio" runat="server" Text="Cancelar" CssClass="btn btn-secondary" OnClick="btnCancelar_Click" CausesValidation="False" />
                          <asp:Button ID="BtnAgregarSocio" ClientIDMode="Static" runat="server"  CausesValidation="false"  Text="Seleccionar" CssClass="btn btn-success" OnClick="BtnAgregarSocio_Click"  />
                      </div>
                </ContentTemplate>
                <!-- 添加Trigger实现外部文本框更新 -->
                <Triggers>
                    <asp:PostBackTrigger ControlID="BtnAgregarSocio" />
                </Triggers>
            </asp:UpdatePanel>
          
        </div>
    </div>
</div>

后台代码

protected void BtnAgregarSocio_Click(object sender, EventArgs e)
{
    TxtSocio.Text = hfidNombreSocio.Value;
    // 可选:添加关闭模态框的脚本
    ScriptManager.RegisterStartupScript(this, this.GetType(), "closeModal", "var myModal = bootstrap.Modal.getInstance(document.getElementById('modalSocio')); myModal.hide();", true);
}


protected void gvSocios_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    // 修复GridView引用错误,把gvFormasPago替换为gvSocios
    var row = gvSocios.Rows[e.NewSelectedIndex];

    BtnAgregarSocio.Enabled = true;
    hfSocio.Value = row.Cells[0].Text;
    hfidNombreSocio.Value = row.Cells[0].Text + " - " + row.Cells[1].Text + " " + row.Cells[2].Text;

}

关联文本框代码

<div class="row">
    <div class="col-md-4">
    <asp:Label ID="lblSocio" runat="server" Visible="false" Text="Socio Cuenta Corriente" CssClass="form-label"></asp:Label>
<asp:TextBox ID="TxtSocio" runat="server" text="0"  CssClass="form-control"  ></asp:TextBox>
             
        <asp:LinkButton ID="btnBuscar" runat="server" Visible="false"  CssClass="btn btn-outline-success" Text="Buscar" CausesValidation="false" ToolTip="Buscar" OnClick="btnBuscar_Click" ><span class="fas fa-search"></span></asp:LinkButton>   <%-- 该按钮用于打开模态框  --%>
    </div>
修复说明
  1. 修复了gvSocios_SelectedIndexChanging事件中错误引用gvFormasPago的问题,替换为当前操作的gvSocios控件
  2. 将模态框底部按钮、所有隐藏域挪到了UpdatePanel的ContentTemplate内部,保证局部回发时按钮状态、隐藏域赋值可以正常同步到前端
  3. 新增PostBackTrigger配置,让BtnAgregarSocio点击时触发全量回发,可正常更新UpdatePanel外部的TxtSocio文本框
  4. 可选添加了模态框关闭的脚本,点击添加按钮后自动关闭模态框

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 13:18:03