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

ASP.NET GridView嵌套级联Dropdownlist出现ControlParameter找不到控件错误

报错原因

因为DropCiudad控件嵌套在GridView的EditItemTemplate模板内,属于GridView行的命名容器下的子控件,页面级的SqlDataSourceComuna直接通过ControlID查找根级控件时,无法定位到行内的DropCiudad,所以抛出找不到控件的错误。

解决方案

按以下步骤修改即可实现级联效果:

1. 修改EditItemTemplate内的DropCiudad配置

给DropCiudad添加自动回发属性和选中变更事件,修改后代码如下:

<asp:DropDownList ID="DropCiudad" runat="server" 
    DataSourceID="SqlDataSourceCiudad" 
    DataValueField="PAR_COD_PAR" 
    DataTextField="PAR_DES_PAR"
    AutoPostBack="True" 
    OnSelectedIndexChanged="DropCiudad_SelectedIndexChanged">
</asp:DropDownList>

2. 调整SqlDataSourceComuna的参数与位置

首先将SqlDataSourceComuna移动到Comuna字段的EditItemTemplate内部,避免多编辑行的参数冲突,同时将原来的ControlParameter改为普通Parameter,移除ControlID相关配置:

<EditItemTemplate>
    <asp:DropDownList ID="DropComuna" runat="server" DataSourceID="SqlDataSourceComuna" DataValueField="PAR_COD_PAR" DataTextField="PAR_DES_PAR"></asp:DropDownList>
    <!-- 数据源移动到此处 -->
    <asp:SqlDataSource ID="SqlDataSourceComuna" runat="server" 
        ConnectionString="<%$ ConnectionStrings:CadenaConexion %>" 
        SelectCommand="SELECT par_cod_par, par_des_par, par_cod_aux FROM PAR WHERE (par_cod_tab = 2) AND (par_cod_aux = @Ciudad)">
        <SelectParameters>
            <asp:Parameter Name="Ciudad" Type="String" DefaultValue="0" />
        </SelectParameters>
    </asp:SqlDataSource>
</EditItemTemplate>

3. 实现后台事件逻辑

RowDataBound事件处理(确保编辑行加载时默认值正确)

在你已有的GridLectores_RowDataBound事件中添加编辑行的绑定逻辑:

protected void GridLectores_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // 只处理编辑状态的行
    if (e.Row.RowType == DataControlRowType.DataRow && GridLectores.EditIndex == e.Row.RowIndex)
    {
        // 找到当前行的DropCiudad
        DropDownList dropCiudad = (DropDownList)e.Row.FindControl("DropCiudad");
        // 找到当前行的SqlDataSourceComuna
        SqlDataSource dsComuna = (SqlDataSource)e.Row.FindControl("SqlDataSourceComuna");
        if (dropCiudad != null && dsComuna != null)
        {
            // 给Comuna的数据源赋值当前选中的Ciudad值
            dsComuna.SelectParameters["Ciudad"].DefaultValue = dropCiudad.SelectedValue;
        }
    }
}

DropCiudad选中变更事件处理(实现级联加载)

protected void DropCiudad_SelectedIndexChanged(object sender, EventArgs e)
{
    // 获取触发事件的DropCiudad所在的当前行
    DropDownList dropCiudad = (DropDownList)sender;
    GridViewRow currentRow = (GridViewRow)dropCiudad.NamingContainer;
    // 找到当前行的SqlDataSourceComuna和DropComuna
    SqlDataSource dsComuna = (SqlDataSource)currentRow.FindControl("SqlDataSourceComuna");
    DropDownList dropComuna = (DropDownList)currentRow.FindControl("DropComuna");
    if (dsComuna != null && dropComuna != null)
    {
        // 更新参数值并重新绑定Comuna下拉框
        dsComuna.SelectParameters["Ciudad"].DefaultValue = dropCiudad.SelectedValue;
        dropComuna.DataBind();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 05:24:03