Asp .Net Dropdownlist Empty After Form Submit

0

I have a dropdownlist that is populated dynamically after two textboxes have been filled. When I submit the form the dropdownlist is empty even though it is filled dynamically.

DropDownList

    <asp:DropDownList ID="DropDownListReason" runat="server" CssClass="span8">
        <asp:ListItem Text="Selecione o motivo da solicitação" Value=""></asp:ListItem>
    </asp:DropDownList>

Render

     protected override void Render(HtmlTextWriter writer)
     {
         //busca lista de dados
         foreach (var item in listaDeDados)
         {  
             Page.ClientScript.RegisterForEventValidation(this.DropDownListReason.UniqueID, item.Id.ToString());
         }

         base.Render(writer);
     }

Jquery

//faz o get dos dados
   $.each(response, function (key, value) {
      if (value.requiresDescription) {
         requiresDescription.push(value.id);
      }

      $("#<%= DropDownListReason.ClientID %>").append($("<option />").val(value.id).text(value.description));
   });

Form submit is an event on the click of a button.

How do I dynamically fill in the data, do they persist in submitting the form?

    
asked by anonymous 10.01.2018 / 14:32

1 answer

1

What happens is that your new item is not in viewstate of the control, so when .net "traces" your page it loses its value.

A good option would be to store this value for example in a hidden field and insert it into the dropdown in Page_Init , since viewstate will only be loaded in Page_Load .

    
10.01.2018 / 17:16