Postback removes selected items Multiple dropdownlist

Asked

Viewed 353 times

0

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

<!-- 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 just updates the grid on the screen, no interaction with ddl’s.

What should I do to keep ddl1 items selected after events are triggered? Remembering that ddl is not loaded dynamically, so 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();
}
  • Where is code Behind?

  • The Behind code is quite dry yet. It only makes a query on the bench and plays in a Gridview. The only part that one of the ddls appears in the code is the following: GridPrincipal.PageSize = int.Parse(dpBuscaPageSize.SelectedItem.Text) Postback calls nothing. Load() is called only when it is ! isPostBack, which in turn throws the dice into Gridview and gave. I believe there is no relevance to this case. ddl2 change calls Load() also.

  • but it has its load and the event that is firing the postback...

  • Ready. I haven’t made the filters yet because this ddl is not behaving as it should, when finishing the screen then yes I do the functional part

2 answers

0

Need to use Updatepanel? Can’t switch to a Panel?

Another thing you can check is if the Viewstate is enabled

  • Worst I need, can not take it not. I checked and the Viewstate is enabled in the full page.

0

Solved

Instead of a Dropdownlist, I used a select. In codeBehind there are few differences but some more treatments are needed. I didn’t understand exactly why, but I kept all the other elements (including Updatepanels) and changed only this one. Items stopped being 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.