Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First Item in dropdownlist doesn't fire SelectedIndexChanged at all

Tags:

c#

asp.net

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>

And this is my code behind

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

    }
}

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

Where I'm doing wrong?

Edit: 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!

like image 390
AaA Avatar asked Jan 02 '12 09:01

AaA


People also ask

How to trigger onselectedindexchanged event in dropdownlist?

So, make sure that you have no repeated values in the list items to trigger this " onselectedindexchanged " event Show activity on this post. Add property ViewStateMode="Enabled" and EnableViewState="true" And AutoPostBack="true" in drop DropDownList

Does the index change when you select the first item?

well i don't think the index has changed when you are selecting the first item, since its actually the same index at which its loaded – V4Vendetta Jan 2 '12 at 9:40

How to write a highly active question in dropdownlist control?

Instead of what you have written, you can write it directly in the SelectedIndexChanged event of the dropdownlist control, e.g. Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

What version of ASP NET is used in dropdownlist autopostback?

This is being served from a Windows Server 2003 R2 machine, running ASP.NET with the .NET Framework version 4. Show activity on this post. Set DropDownList AutoPostBack property to true.


2 Answers

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>
like image 98
hentie Avatar answered Oct 19 '22 22:10

hentie


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].
like image 38
AaA Avatar answered Oct 19 '22 21:10

AaA