简体   繁体   English

刷新页面后,下拉列表中的项目丢失,asp.net的下拉列表中只有一个项目

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

相关问题 页面回发或刷新后在ASP.NET中刷新通用列表 - Refresh Generic List in ASP.NET after page is postback or refreshed 从ASP.NET C#中的另一个下拉列表中选择项目后如何将项目加载到下拉列表中 - How to load items into dropdownlist after selecting item from another dropdownlist in asp.net c# 建立并运行asp.net网站后,我的页面自动刷新一次 - After building and running asp.net website my page get refreshed automatically one time 我如何对一个下拉列表进行更改以反映在asp.net中的其他下拉列表上? - How do i make changes of one dropdownlist reflect on other dropdown list in asp.net? asp.net中的下拉列表项选择 - dropdown list item selection in asp.net ASP.net C#页面,页面上有2个DropDownList,其中2个onSelectedIndexChange仅第一个触发 - ASP.net C# page, 2 DropDownList on page with 2 onSelectedIndexChange only the first one fires 在ASP.NET中刷新页面后如何将用户添加的项目带入下拉列表? - How to bring user-added item to dropdownlist after page refresh in asp.net? 样式ASP.net DropDownList不选择元素下拉列表 - Style ASP.net DropDownList Not Select Element DropDown List 自动 Select 下拉列表中的一项 asp.net c# - Auto Select one item on Dropdownlist asp.net c# 在c#asp.net中启用DropdownList的列表项 - Enabling List item of DropdownList in c# asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM