PostBack removes selected items Multiple DropDownList

0

I have a static Multiple DropDownList (ddl1) on my aspx page. It has 4 options, from which 0 to 4 options can be selected. Another DropDownList (ddl2) that is also static on the form has the OnSelectedIndexChanged event and when this event is triggered, dll1 loses multiple selections, keeping only the first option selected (or none if the user does not select it).

<!-- ddl1: -->

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Panel ID="PanelPesquisa" runat="server" CssClass="form-group">

<!--  [...] Alguns Campos de Busca [...] -->

    <asp:DropDownList ID="dpBuscaEtapasCapacitacao" runat="server" 
CausesValidation="false" ValidationGroup="BuscarQuestao" 
ClientIDMode="Static" CssClass="chosen-select form-control" data- 
placeholder="Selecione as Etapas" multiple="multiple" TabIndex="4">
        <asp:ListItem Text="Nivelamento" Value="0"></asp:ListItem>
        <asp:ListItem Text="Capacitação EaD" Value="1"></asp:ListItem>
        <asp:ListItem Text="Capacitação Presencial" Value="2"> 
</asp:ListItem>
        <asp:ListItem Text="Atualização" Value="3"></asp:ListItem>
    </asp:DropDownList>

<!-- ddl2: -->

    <asp:DropDownList ID="dpBuscaPageSize" runat="server" CssClass="form- 
control" AutoPostBack="true" CausesValidation="true" 
OnSelectedIndexChanged="BuscaDados" ClientIDMode="Static">
        <asp:ListItem Text=""></asp:ListItem>
        <asp:ListItem Text="5"></asp:ListItem>
        <asp:ListItem Text="10"></asp:ListItem>
    </asp:DropDownList>

        </asp:Panel>

<!--  [...] Muito HTML depois [...] -->

    </ContentTemplate>
</asp:UpdatePanel>

The event only updates the grid on the screen, without interaction with ddl's.

What should I do to keep ddl1 items selected after events are triggered? Remembering that ddl is not loaded dynamically, then there is no way I can "call the function in postback" because there is no function.

Code Behind:

protected override void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        CarregaDados();
}

private void CarregaDados(){
    var lst = consulta_no_banco(" - sql_query - ");

    GridPrincipal.PageSize = int.Parse(dpBuscaPageSize.SelectedItem.Text);
    GridPrincipal.DataSource = lst;
    GridPrincipal.DataBind();
}
/*
    Esta função é chamada em todos os eventos dos Controls de filtro da tela.
    Cada TextChanged e SelectedItemChanged do filtro é direcionado para cá, mas 
    ainda não estou fazendo o filtro em sí.
*/
protected void BuscaDados(object sender, EventArgs e)
{
    CarregaDados();
}
    
asked by anonymous 31.07.2018 / 15:25

2 answers

0

Do you need to use UpdatePanel? Can not switch to Panel?

Another thing you can check is whether ViewState is enabled

    
31.07.2018 / 18:54
0

Resolved

Instead of a DropDownList, I used a select. In codeBehind there are few differences but some more treatments are needed. I did not understand the exact reason but kept all the other elements (including UpdatePanels) and just changed this one. Items have been reset!

<select id="dpModulosLicenciados" runat="server" class="chosen-select form-control" data-placeholder="Selecione os Módulos" multiple="true" tabindex="4"></select>

Life that follows my people. The important thing is that it works

    
13.08.2018 / 19:16