本文介绍了在DropDownList中的第一项不火在所有的SelectedIndexChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的code:

I have following simple code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testForm.aspx.cs" Inherits="Orbs.testForm" %>
<html>
<body>
    <form id="form1" runat="server">
        <asp:DropDownList ID="dropdown1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdown1_SelectedIndexChanged" ViewStateMode="Enabled">
            <asp:ListItem Value="1" Text="Item 1" />
            <asp:ListItem Value="2" Text="Item 2" />
            <asp:ListItem Value="3" Text="Item 3" />
            <asp:ListItem Value="4" Text="Item 4" />
            <asp:ListItem Value="5" Text="Item 5" />
        </asp:DropDownList>
        <asp:Label runat="server" ID="label1"></asp:Label>
    </form>
</body>
</html>

这是背后

using System;

namespace Orbs {
    public partial class testForm: System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            label1.Text = "???!!";
        }

        protected void dropdown1_SelectedIndexChanged(object sender, EventArgs e) {
            label1.Text = "Fired on " + dropdown1.SelectedValue;
        }

    }
}

当我第一次进入页面后,卷标1 显示??? !! 。现在我选择下拉列表中的项目和卷标1 显示正确的值,但是当我选择下拉列表中的第一个项目,它再次显示了 ??? !! 而不是射击1

When the first time I enter the page, label1 shows '???!!'. Now I select an item from dropdown and label1 shows correct value but when I select first item in dropdown, it again shows ???!! instead of Fired on 1

我哪里做错了吗?

编辑:我发现如果我添加选择=真任何在下拉列表中的项目,该项目成为受害者,也不会触发事件

I noticed if I add Selected="True" to any of the items in the dropdown, that item becomes victim and won't fire the event!

推荐答案

我解决了自己的问题,

我读的地方,关闭 ViewStateMode 将导致的DropDownList 无法正常工作。在我的web应用程序,我不得不关掉 ViewStateMode 来实现一些全局任务和逐案开启的情况下。

I read somewhere that turning off the ViewStateMode will cause DropDownListnot work properly. In my web application I had to turn off ViewStateMode to achieve some global task and turn it on case by case.

的DropDownList 不工作,我甚至试过不知怎的打开 ViewStateMode 打开 ViewStateMode 的页面和主页面,但仍的DropDownList 没有工作。它只有当我在的web.config 打开 ViewStateMode 工作。

Somehow turning on ViewStateMode on DropDownList is not working, I even tried turning on ViewStateMode for page and master page but still DropDownList didn't work. it only worked when I turned on ViewStateMode in web.config.

作为的web.config打开 ViewStateMode 是不是一种选择,我发现另一种解决方案。我包括在这里希望它帮助别人。

As turning on ViewStateMode in web.config is not an option, I found and alternate solution. I'm including it here hoping it help someone.


  • HiddenField 添加到您的窗体。

  • 的Page_Load 比较 HiddenField 的价值与 Request.Forms [DropDownList1.UniqueID]

  • 如果他们是不同的,叫的SelectedIndexChanged 手动

  • 设置 HiddenField 的值的值 Request.Forms [DropDownList1.UniqueID]

  • Add a HiddenField to your form.
  • In Page_Load compare value of HiddenField with Request.Forms[DropDownList1.UniqueID]
  • if they are different, call SelectedIndexChanged manually
  • Set the value of HiddenField to value of Request.Forms[DropDownList1.UniqueID].

这篇关于在DropDownList中的第一项不火在所有的SelectedIndexChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 12:48