I have following simple code:
<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"testForm.aspx.cs\" Inherits=\"Orbs.testForm\" %>
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 = "???!!";
}
}
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).
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.
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 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]
.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