ASP.NET DropDownList在回發時不保留選定的項目

[英]ASP.NET DropDownList not retaining selected item on postback


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)

我有一個ASP DropDownList,它在Page_Load事件中填充,在我選擇一個項目並點擊一個按鈕后,所選項目被清除,並且DropDownList中的第一個項目被選中。 (DropDownList僅在頁面不回發時填充)

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;
            }
    }

2 个解决方案

#1


31  

The page lifecycle does the following (plus other steps irrelevant to your question):

頁面生命周期執行以下操作(以及與您的問題無關的其他步驟):

  1. OnInit
  2. 的OnInit
  3. Populate controls from ViewState (during postback)
  4. 從ViewState填充控件(在回發期間)
  5. Set the selected values (during postback)
  6. 設置選定的值(在回發期間)
  7. Page_Load
  8. 的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 }

您需要啟用ViewState,以便在“選擇”項目之前填充列表。在這種情況下,請確保您不在Page_Load中重新填充並丟失所選值。像if(!IsPostback){//填充}這樣的事情

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.

否則,您必須在每個頁面請求的OnInit事件中手動填充列表。 Page_Load在生命周期中為時已晚,因此所選項目將丟失。

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:

DropDownList還必須設置有效值(與瀏覽器中顯示的文本分開)。這是通過DataValueField屬性完成的。每個值必須是唯一的,否則只會選擇第一個重復項。如果您在瀏覽器中查看HTML源代碼,您應該:

<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.

唯一值用於在服務器端選擇正確的項目。

#2


3  

Are u using a Master Page? If so, remember to put the EnableViewState on true in the master page.

你使用的是母版嗎?如果是這樣,請記住在母版頁中將EnableViewState設置為true。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/11/15/699604fc244a573bee541f314516c66f.html



 
  © 2014-2022 ITdaan.com 联系我们: