[英]Items in dropdownlist is lost , only one item is coming in dropdown list in asp.net after page is refreshed
I have a dropdown on the page in asp.net, which contains list of countries. 我在asp.net的页面上有一个下拉列表,其中包含国家/地区列表。 When the page is loaded, I have bound the datatable to dropdown list.
加载页面时,我已将数据表绑定到下拉列表。 On page load, all the countries list is populated in the dropdown list.
在页面加载时,所有国家列表都将填充在下拉列表中。 Then, once I refresh the page, only India is populated in the dropdown list.Can anyone help me with the issue?
然后,刷新页面后,下拉列表中仅填充印度。有人可以帮助我解决此问题吗? Thanks in advance.
提前致谢。
Here is the code : Design : 这是代码:设计:
<asp:DropDownList ID="ddlCountry" runat="server" TabIndex="7"
placeholder="by Country" AutoPostBack="true"
OnSelectedIndexChanged = "ddlCountry_SelectedIndexChanged">
</asp:DropDownList>
Code Behind : 背后的代码:
DataSet CountryMasters =Global.DsCountry;//(DataSet)Application["CountryMasters"];
DataTable dtcountry = CountryMasters.Tables[0];
ddlcountry.Items.Clear();
ddlcountry.DataSource = dtcountry;
ddlcountry.DataTextField = "CountryName";
ddlcountry.DataValueField = "CountryCode";
ddlcountry.DataBind();
ddlcountry.Items.Insert(0, new ListItem("Select", ""));
Try wrapping your initialisation code with a conditional that checks if the form is being posted back. 尝试使用一个条件检查包装您的初始化代码,该条件检查是否回发了表单。
if (!this.IsPostBack)
{
DataSet CountryMasters =Global.DsCountry;//(DataSet)Application["CountryMasters"];
DataTable dtcountry = CountryMasters.Tables[0];
ddlcountry.Items.Clear();
ddlcountry.DataSource = dtcountry;
ddlcountry.DataTextField = "CountryName";
ddlcountry.DataValueField = "CountryCode";
ddlcountry.DataBind();
ddlcountry.Items.Insert(0, new ListItem("Select", ""));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.