Asp . Empty Net Dropdownlist After Form Submit

Asked

Viewed 133 times

0

I own a dropdownlist which is dynamically filled after two textboxes have been filled in. When I do form Ubmit the dropdownlist is empty, even if dynamically filled.

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));
   });

The form Submit is a one button click event.

How do I make sure that when dynamically filling the data, it persists in the form Ubmit ?

  • What do you mean persist in Submit? Keep value after page reload?

  • The form is posted by clicking a button that triggers an on click event, in this event the dropdownlist is already empty, it should be filled in to be validated and go to a new page.

  • I believe that if you are populating your dropdown in the client, this control needs to be within a <form> tag and you will only recover this value on the server through a Request

  • This I fill on the client side, the dropdownlist tag is inside the <form> tag, How to do this last step that spoke ?

  • Adds the method where you are retrieving form information after Submit

1 answer

1


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

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

  • It wasn’t exactly what I did to solve, but it gave me a great idea.

  • Great, if you can, share your idea to help anyone with the same problem and get to this topic. Anyway, changes in content by javascript and viewstate do not talk very well... In the vast majority of times it is necessary to store the data in a hiddenfield to then manipulate the value in code Behind.

Browser other questions tagged

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