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!
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
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
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.
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.
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>
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.
HiddenField to your form. Page_Load compare value of HiddenField with Request.Forms[DropDownList1.UniqueID] SelectedIndexChanged manually HiddenField to value of Request.Forms[DropDownList1.UniqueID].If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With