ASP.NET DropDownList 在回发时不保留所选项目

发布于 2024-10-02 05:47:47 字数 1259 浏览 2 评论 0原文

我有一个 ASP DropDownList,它在 Page_Load 事件上填充,在我选择一个项目并单击按钮后,所选项目将被清除,并且 DropDownList 中的第一个项目将被选择。 (只有当页面没有回发时才填充DropDownList)

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

I have an 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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

用心笑 2024-10-09 05:47:47

页面生命周期执行以下操作(以及与您的问题无关的其他步骤):

  1. OnInit
  2. 从 ViewState 填充控件(在回发期间)
  3. 设置选定的值(在回发期间)
  4. Page_Load

您需要启用 ViewState,以便它可以在“选择”项目之前填充列表。在这种情况下,请确保您不会在 Page_Load 中重新填充并丢失所选值。执行类似 if (!IsPostback) { // Populate } 的操作

,否则,您必须在每个页面请求的 OnInit 事件中手动填充列表。 Page_Load 在生命周期中为时已晚,因此所选项目会丢失。

编辑:

DropDownList 还必须设置有效值(与浏览器中显示的文本分开)。这是通过 DataValueField 属性完成的。每个值必须是唯一的,否则只会选择第一个重复的项目。如果您在浏览器中查看 HTML 源代码,您应该具有:

<select>
    <option value="unique_value1">Displayed text1</option>
    <option value="unique_value2">Displayed text2</option>
</select>

唯一值用于在服务器端选择正确的项目。

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

  1. OnInit
  2. Populate controls from ViewState (during postback)
  3. Set the selected values (during postback)
  4. 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.

不一样的天空 2024-10-09 05:47:47

您使用母版页吗?如果是这样,请记住在母版页中将 EnableViewState 设置为 true。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文