[英]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.