ASP.NET的DropDownList不保留在回发选中的项目 [英] ASP.NET DropDownList not retaining selected item on postback
问题描述
我有一个ASP DropDownList中的被填充在Page_Load事件后,我选择一个项目,并触及按键选定项目被清除,并在DropDownList的第一项被选中。 (当页面不回发DropDownList控件中只填入)
请帮助
如果(!的IsPostBack)
{
清单<国家> lCountries =新的List<国家>();
清单< CompanySchedule> lCompanySchedules =新的List< CompanySchedule>();
this.Load_Countries(lCountries);
this.Load_Schedules(lCompanySchedules);
如果(personnelRec == NULL)
{personnelRec =新员工(); }
如果(的Request.QueryString [UA] = NULL&放大器;!&安培; Convert.ToInt32(的Request.QueryString [UA]的ToString())方式> 0)
{
userAccount.ID = Convert.ToInt32(的Request.QueryString [UA]的ToString());
App_Database.Snapshift_Select_Helper.SNAPSHIFT_SELECT_PERSONNEL_APP_ACCOUNT(REF userAccount);
}
this.imgEmployeePicture.ImageUrl =./images/Employees/nophoto.gif;
如果(的Request.QueryString [EI] = NULL&放大器;!&安培; Convert.ToInt32(的Request.QueryString [EI]的ToString())方式> 0)
{
this.Load_PersonnelRec(Convert.ToInt32(的Request.QueryString [EI]的ToString()));
}
其他
{
this.lblChangeDirectionHead.Enabled = FALSE;
this.lblChangeDirections.Enabled = FALSE;
this.lbSchedules.Disabled = TRUE;
}
}
页面生命周期执行以下操作(不相干加上其他措施,你的问题):
-
的OnInit
- 从视图状态(回发期间)填充控制
- 将选定的值(回发期间)
-
的Page_Load
您需要启用ViewState中,因此它可以填充列表之前,选择的项目。在这种情况下,请务必不要在Page_Load中重新填充,失去选择的值。做这样的事情如果(!的IsPostBack){//填充}
否则,您必须手动填充每个页面请求的OnInit
事件列表。 的Page_Load
为时已晚的生命周期中,所以选取的物品丢失。
编辑:
的的DropDownList
必须也有效值设置(从浏览器中显示的文本分开)。这是通过的 DataValueField属性来实现。每个值必须是唯一的,否则只有第一个重复的项目都不会被选中。如果你看看在你的浏览器的HTML源代码,你应该有:
<选择>
<期权价值=unique_value1>显示文本1< /选项>
<期权价值=unique_value2>显示文本2< /选项>
< /选择>
的唯一值被用于选择在服务器侧的右项目
I have a ASP DropDownList that gets populated on the Page_Load event, after i select an item and hit a button the selected item gets cleared and the first item in the DropDownList gets selected. (The DropDownList is only populated when the page is not postback)
Help please
if (!IsPostBack)
{
List<Country> lCountries = new List<Country>();
List<CompanySchedule> lCompanySchedules = new List<CompanySchedule>();
this.Load_Countries(lCountries);
this.Load_Schedules(lCompanySchedules);
if (personnelRec == null)
{ personnelRec = new Personnel(); }
if (Request.QueryString["UA"] != null && Convert.ToInt32(Request.QueryString["UA"].ToString()) > 0)
{
userAccount.ID = Convert.ToInt32(Request.QueryString["UA"].ToString());
App_Database.Snapshift_Select_Helper.SNAPSHIFT_SELECT_PERSONNEL_APP_ACCOUNT(ref userAccount);
}
this.imgEmployeePicture.ImageUrl = "./images/Employees/nophoto.gif";
if (Request.QueryString["EI"] != null && Convert.ToInt32(Request.QueryString["EI"].ToString()) > 0)
{
this.Load_PersonnelRec(Convert.ToInt32(Request.QueryString["EI"].ToString()));
}
else
{
this.lblChangeDirectionHead.Enabled = false;
this.lblChangeDirections.Enabled = false;
this.lbSchedules.Disabled = true;
}
}
The page lifecycle does the following (plus other steps irrelevant to your question):
OnInit
- Populate controls from ViewState (during postback)
- Set the selected values (during postback)
Page_Load
You need to have ViewState enabled so it can populate the list before it "selects" the item. In this case, make sure you don't repopulate in Page_Load and lose the selected value. Do something like if (!IsPostback) { // Populate }
Otherwise, you have to populate the list manually in the OnInit
event on every page request. Page_Load
is too late in the lifecycle, so the selected item is lost.
Edit:
The DropDownList
must also have valid values set (separate from the text displayed in the browser). This is done through the DataValueField
property. Each value must be unique, otherwise only the first duplicate item will ever be selected. If you look at the HTML source in your browser, you should have:
<select>
<option value="unique_value1">Displayed text1</option>
<option value="unique_value2">Displayed text2</option>
</select>
The unique values are used for selecting the right item on the server side.
这篇关于ASP.NET的DropDownList不保留在回发选中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!