简体   繁体   English

表单提交后,Asp .NET Dropdownlist为空

[英]Asp .NET Dropdownlist Empty After Form Submit

I have a dropdownlist that is filled dynamically after two textboxes being filled. 我有一个dropdownlist ,在填充两个textboxes后动态填充。 When I submit the form, the dropdownlist it's empty even after being filled dynamically. 当我提交表单时,即使在动态填充后, dropdownlist也是空的。

DropDownList 下拉列表

<asp:DropDownList ID="DropDownListReason" runat="server" CssClass="span8">
    <asp:ListItem Text="Select a reason" Value=""></asp:ListItem>
</asp:DropDownList>

Render 给予

 protected override void Render(HtmlTextWriter writer)
 {
     //get the data list.
     foreach (var item in dataList)
     {  
         Page.ClientScript.RegisterForEventValidation(this.DropDownListReason.UniqueID, item.Id.ToString());
     }

     base.Render(writer);
 }

Jquery jQuery的

//get the data
   $.each(response, function (key, value) {
      if (value.requiresDescription) {
         requiresDescription.push(value.id);
      }

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

The form submmit action it's a button on click event. 表单submmit action是click事件上的一个按钮。

How can I persist the dropdownlist data after the form being submited ? 在提交表单后如何保留dropdownlist数据?

You need to fill the DropDownList in code behind also. 您还需要在后面的代码中填充DropDownList。 The backed does not know you filled it with javascript, so when you perform a PostBack the page is reloaded and original state is restored, which is no ListItems. 支持不知道你用javascript填充它,所以当你执行PostBack时,页面被重新加载并恢复原始状态,这不是ListItems。

So either add all the options in the DropDownList. 因此,要么添加DropDownList中的所有选项。

<asp:DropDownList ID="DropDownListReason" runat="server" CssClass="span8">
    <asp:ListItem Text="Select a reason" Value=""></asp:ListItem>
    <asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
</asp:DropDownList>

Or add them programatically 或者以编程方式添加它们

<asp:DropDownList ID="DropDownListReason" runat="server" AppendDataBoundItems="true" CssClass="span8">
    <asp:ListItem Text="Select a reason" Value=""></asp:ListItem>
</asp:DropDownList>

DropDownListReason.Items.Insert(DropDownListReason.Items.Count(), new ListItem("Option 1", "1"));
DropDownListReason.Items.Insert(DropDownListReason.Items.Count(), new ListItem("Option 2", "2"));

Now when you submit the page you can read the value of DropDownListReason with DropDownListReason.SelectedValue . 现在,当您提交页面时,您可以使用DropDownListReason.SelectedValue读取DropDownListReason的值。 This retaining of form values across PostBack is called ViewState . 在PostBack中保留表单值称为ViewState If you're gonna work with webforms you should read up on that. 如果你要使用webforms,你应该阅读它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 mvc 4 ASP.net 中提交表单后保存下拉列表状态和单选按钮状态 - Save dropdownlist state and Radio Button state after form submit in mvc 4 ASP.net 单击提交按钮后,ASP.NET ComboBox为空 - ASP.NET ComboBox is empty after submit button is clicked 提交后ASP.Net MVC视图下拉列表未绑定到模型(帖子) - ASP.Net MVC view dropdownlist not being binded to model after submit (post) 如何仅在按下提交按钮时提交ASP.NET表单,而不是通过自动回发更改下拉列表的选定索引时才提交ASP.NET表单? - How to submit ASP.NET form only when the submit button is pressed and not when the selected index is changed of a dropdownlist with autopostback? 在Asp.net Core中提交ajax表单后的RedirectToAction - RedirectToAction after ajax form submit in Asp.net Core 在 ASP.NET MVC 5 中提交表单后返回到同一视图 - Return to same view after form submit in ASP.NET MVC 5 ASP.Net表单提交 - ASP.Net Form submit Asp .net 下拉列表 SelectedItem 在下拉列表中清除后不会更改 - Asp .net dropdownlist SelectedItem doesn't change after clear in dropdownlist 带有提交按钮回发的ASP.Net MVC Razor Dropdownlist - ASP.Net MVC Razor Dropdownlist with submit button postback 在 ASP.NET MVC 项目中提交选定的下拉列表值 - Submit selected dropdownList value in ASP.NET MVC project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM