First Item in dropdownlist doesn't fire SelectedIndexChanged at all

后端 未结 7 2049
盖世英雄少女心
盖世英雄少女心 2021-02-20 12:24

I have following simple code:

<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"testForm.aspx.cs\" Inherits=\"Orbs.testForm\" %>

         


        
相关标签:
7条回答
  • 2021-02-20 12:33

    This is happening because you are setting the label to "???!!" every page event.

    You need to modify your page load to detect f a Postback has not occured.

        protected void Page_Load(object sender, EventArgs e) {
            if(!IsPostBack)
            {
                label1.Text = "???!!";
            }
        }
    
    0 讨论(0)
  • 2021-02-20 12:35

    This question covers all the possibilities better than other posts out there, so I'm adding this explicit answer. In my case @Edyn's comment worked, even though the original problem already has this:

    Set ViewStateMode="Enabled" on the dropdown control itself.
    I also set it on the page declaration at the top of the page, just in case.

    This is .Net 4.0, so perhaps something was changed (but certainly not fixed nicely).

    0 讨论(0)
  • 2021-02-20 12:40

    I had the same problem, but I solved it by calling onindexchanged function manually, like this:

    ddl_SelectedIndexChanged(null, null);
    

    I know this may not be the perfect way but it's working for me.

    0 讨论(0)
  • 2021-02-20 12:44

    For Anyone still having the problem; I solved it in a different, yet easier way: Just add a dummy ListItem to the start of the DropDownList and set that item's Enabled property to false. i.e.

    <asp:DropDownList ID="dropdown1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdown1_SelectedIndexChanged" ViewStateMode="Enabled">
            <asp:ListItem Value="" Text="" Enabled="false" />
            <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>
    
    0 讨论(0)
  • 2021-02-20 12:45

    I solved the problem myself,

    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.

    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.

    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.

    • 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].
    0 讨论(0)
  • 2021-02-20 12:49

    Had the same issue - SelectedIndexChanged doesn't fire when selecting the first option, My not clean solution was (not sure that was so smart but it work for me),

    At the Page_Load I added the follow script:

    if (!IsPostBack)
            {
    
                //bind data first time
            }
            else
            {
                int ddlSortByValue = int.Parse(ddlSortBy.SelectedValue);
                if (ddlSortByValue == 0)
                {
                    ddlSortBy_SelectedIndexChanged(this, EventArgs.Empty);
                }
    
            }
    

    That way I force the SelectedIndexChanged event to fire up

    0 讨论(0)
提交回复
热议问题