Reputation: 3694
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!
Upvotes: 11
Views: 16408
Reputation: 2848
In my case, this happened because I had referred to that dropdown using a local dropdownlist variable, and disposed off that variable at the end of a function. I was adding the first item to the dropdownlist after setting a datasource. Took me 2 hours to find this, and worked perfectly after I removed the dispose call.
Upvotes: 0
Reputation: 109
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.
Upvotes: 1
Reputation: 2449
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).
Upvotes: 1
Reputation: 3694
I solved the problem myself,
I read somewhere that turning off the ViewStateMode
will cause DropDownList
not 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]
.Upvotes: 4
Reputation: 181
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>
Upvotes: 18
Reputation: 474
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
Upvotes: 1
Reputation: 7539
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 = "???!!";
}
}
Upvotes: 0