DropDownList - SelectedIndexChanged doesn't fire

D

Damien

Hi guys,

I'm trying to learn ASP.NET and got a problem with DroDownList...
SelectedIndexChanged doesn't fire.
Maybe you'll be able to suggest something.

I think it may be connected with the fact I fill this list dynamically.

I use the following code:

--presentation--
<%@ Page Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeFile="index.aspx.cs"
Inherits="PagingAndSorting_index" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent"
Runat="Server">
<br />
<asp:Label ID="PageInfo" runat="server"
Text="Label"></asp:Label>&nbsp;<asp:DropDownList ID="PageList"
runat="server" AutoPostBack="True"
OnSelectedIndexChanged="PageList_SelectedIndexChanged">
</asp:DropDownList><br />
<br />
<asp:GridView ID="Products" runat="server"
AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="ObjectDataSource1" AllowPaging="True"
OnDataBound="Products_DataBound">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Product
Name" SortExpression="ProductName" />
<asp:BoundField DataField="CategoryName"
HeaderText="Category" ReadOnly="True" SortExpression="CategoryName" />
<asp:BoundField DataField="SupplierName"
HeaderText="Supplier" ReadOnly="True" SortExpression="SupplierName" />
<asp:BoundField DataField="UnitPrice" HeaderText="Price"
SortExpression="UnitPrice" />
<asp:CheckBoxField DataField="Discontinued"
HeaderText="Discontinued" SortExpression="Discontinued" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetProducts"
TypeName="ProductsBLL"></asp:ObjectDataSource>
</asp:Content>
---------------

-----logic----
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PagingAndSorting_index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void Products_DataBound(object sender, EventArgs e)
{
PageInfo.Text = string.Format("Page {0}/{1}",
Products.PageIndex + 1, Products.PageCount);

PageList.Items.Clear();

for (int i = 0; i < Products.PageCount; i++)
{
ListItem pageListItem = new ListItem(string.Concat("Go to
page: ", i + 1), i.ToString());
PageList.Items.Add(pageListItem);

if (i == Products.PageIndex)
pageListItem.Selected = true;
}
}

protected void PageList_SelectedIndexChanged(object sender,
EventArgs e)
{
Products.PageIndex = Convert.ToInt32(PageList.SelectedValue);
}
}
 
M

Mark Rae

I'm trying to learn ASP.NET and got a problem with DroDownList...
SelectedIndexChanged doesn't fire.
Maybe you'll be able to suggest something.

I think it may be connected with the fact I fill this list dynamically.

Hmm - well, you specify AutoEventWireup="true" in your Page directive, and
you have set the AutoPostBack property of the DropDownList control to
"true", so it can't be either of those...
protected void PageList_SelectedIndexChanged(object sender, EventArgs e)
{
Products.PageIndex = Convert.ToInt32(PageList.SelectedValue);
}
}

Are you absolutely certain the event isn't firing...? E.g. if you put a
breakpoint in the above code, does it jump into it...?

When you drop the DropDownList control and select a different option, does
the screen "flash" is if it's posting back...?
 
D

Damien

Are you absolutely certain the event isn't firing...? E.g. if you put a breakpoint in the above code, does it jump into it...?

I'm sure. I put breakpoint inside the event but it is never reached.
When you drop the DropDownList control and select a different option, does the screen "flash" is if it's posting back...?

Yes, the screen "flash", so page is posting back.

Another strange thing is when I add another dropdownlist to the form,
then add a few items to it... its SelectedIndexChanged event fires no
matter which dropdownlist I use to select value.

The same event for both DDL... how is it possible?
 
M

Mark Rae

I'm sure. I put breakpoint inside the event but it is never reached.

Hmm - OK...
Yes, the screen "flash", so page is posting back.

Well that's a start, at least... :)
Another strange thing is when I add another dropdownlist to the form,
then add a few items to it... its SelectedIndexChanged event fires no
matter which dropdownlist I use to select value.

Ah yes, now this is a particularly irritating bug...

The only way I've found of working round this is to wrap the entire code
inside the DropDownList's SelectedIndexChanged event with this:

if (Request.Form["__EVENTTARGET"] == PageList.UniqueID)
{
// rest of code goes here
}

The event still fires in response to postbacks from other controls, but none
of its code will run unless the actual DropDownList itself initiated the
postback...

I'm actually quite pleased that you're experiencing this (if you see what I
mean!), because Microsoft Technical Support as well as a couple of the MVPs
in here flatly refuse to acknowledge that this bug exists...
 
D

Damien

Thank for the clue (good one).
I will try this. However I think there is something wrong with the
following code:

--
//clear out all of the items
PageList.Items.Clear();

for (int i = 0; i < Products.PageCount; i++)
{
ListItem pageListItem = new ListItem(string.Concat("Page ", i + 1),
i.ToString());
PageList.Items.Add(pageListItem);

if (i == Products.PageIndex) pageListItem.Selected = true;
}
--

If I add DDL items in VS environment, everything works fine.

Maybe, if I find out what is wrong with the above fragment, the event
problem will solve automatically.
MS products amaze me all the time, you know :)
 
M

Mark Rae

However I think there is something wrong with the following code:

Like what...?
--
//clear out all of the items
PageList.Items.Clear();

for (int i = 0; i < Products.PageCount; i++)
{
ListItem pageListItem = new ListItem(string.Concat("Page ", i + 1),
i.ToString());
PageList.Items.Add(pageListItem);

if (i == Products.PageIndex) pageListItem.Selected = true;
}
--

The only thing I might have done differently would be to evaluate whether
the item should be selected *before* adding it to the Items collection, e.g.

if (i == Products.PageIndex) pageListItem.Selected = true;
PageList.Items.Add(pageListItem);
 
D

Damien

However I think there is something wrong with the following code:
Like what...?

No idea. But all works just fine when I create all items by hand.
Maybe I should place this code within different event. I'll work on
this.
The only thing I might have done differently would be to evaluate whether
the item should be selected *before* adding it to the Items collection, e.g.

if (i == Products.PageIndex) pageListItem.Selected = true;
PageList.Items.Add(pageListItem);

Any particular reason why?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top