Cascading databound <ajaxtoolkit:combobox> and <asp:dropdownlist> in asp.net

I have an asp.net search form that includes an ajaxToolkit Combobox and a standard asp DropDownList. Both controls are bound to two separated SqlDatasource components.

Something like this:

<ajaxToolkit:ComboBox
    ID="cbConvenzionato"
    runat="server"
    AutoCompleteMode="SuggestAppend"
    DropDownStyle="DropDownList"
    DataSourceID="sdsConvenzionati"
    DataTextField="nome"
    DataValueField="id"
    AutoPostBack="true"
    OnSelectedIndexChanged="cbConvenzionato_SelectedIndexChanged" />

<asp:DropDownList
    ID="ddlVeicoli"
    DataSourceID="sdsVeicoli"
    DataTextField="targa"
    DataValueField="id"
    runat="server"
    AutoPostBack="true"
    OnSelectedIndexChanged="ddlVeicoli_SelectedIndexChanged"
    AppendDataBoundItems="true">
    <asp:ListItem Text="TUTTI" Value="" Selected="True" />
</asp:DropDownList>

<asp:SqlDataSource
    ID="sdsConvenzionati"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:db %>"
    ProviderName="<%$ ConnectionStrings:db.ProviderName %>"
    SelectCommand="
        SELECT
            id,
            nome
        FROM
            anag_convenzionati
        ORDER BY nome;" />

<asp:SqlDataSource
    ID="sdsVeicoli"
    runat="server"
    EnableCaching="false"
    CancelSelectOnNullParameter="false"
    ConnectionString="<%$ ConnectionStrings:db %>"
    ProviderName="<%$ ConnectionStrings:db.ProviderName %>"
    SelectCommand="
        SELECT 
            id, 
            targa
        FROM 
            veicoli_contratti
        WHERE
            ((@id_convenzionato IS NULL) OR (id_convenzionato = @id_convenzionato))
        ORDER BY targa;">
    <SelectParameters>
        <asp:ControlParameter
            Name="id_convenzionato"
            ControlID="cbConvenzionato"
            PropertyName="SelectedValue"
            Direction="Input"
            ConvertEmptyStringToNull="true"
            DbType="Int32"
            DefaultValue="" />
    </SelectParameters>
</asp:SqlDataSource>

There's also a third sqldatasource (sdsNoleggi) that feeds a gridview but this's not a problem right now.

In code behind I have two event handlers:

    protected void cbConvenzionato_SelectedIndexChanged(object sender, EventArgs e)
    {
        sdsVeicoli.Select(DataSourceSelectArguments.Empty);

        Search();
    }

    protected void ddlVeicoli_SelectedIndexChanged(object sender, EventArgs e)
    {
        Search();
    }

    private void Search()
    {
        sdsNoleggi.Select(DataSourceSelectArguments.Empty);
    }

I tought in this way I should filter ddlVeicoli items after selecting an item in cbConvenzionato... but it's not working... why?

If I look into sdsVeicoli SelectParameters in debug I can see id_convenzionato being correctly set to selected value (id coming from cbConvenzionato) I bet also that sdsNoleggi dataset wiil be correctly updated with new values since I did this many times before. So why bound control it's not? I tried also to force a ddlVeicoli.DataBind() after sdsVeicoli.Select() call ... but this had no effect.

1 answer

  • answered 2021-04-22 09:43 weirdgyn

    I fixed it this way:

    protected void cbConvenzionato_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlVeicoli.DataSource = null;
    
        sdsVeicoli.Select(DataSourceSelectArguments.Empty);
    
        ddlVeicoli.DataSource = sdsVeicoli;
        ddlVeicoli.DataBind();
    
        Search();
    }