473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

DropDownList - SelectedIndexChanged doesn't fire

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_SelectedIndexChan ged">
</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);
}
}
---------------

--
Thanks in advance
Damien

Jan 26 '07 #1
7 12039
"Damien" <no**************@recursor.netwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
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...?
Jan 26 '07 #2
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?

Jan 26 '07 #3
"Damien" <no**************@recursor.netwrote in message
news:11*********************@q2g2000cwa.googlegrou ps.com...
>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.
Hmm - OK...
>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.
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...
Jan 26 '07 #4
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 :)

--
Thanks a lot
Damien

Jan 26 '07 #5
"Damien" <no**************@recursor.netwrote in message
news:11*********************@v33g2000cwv.googlegro ups.com...
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);
Jan 26 '07 #6
>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.
>--
//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);
Any particular reason why?

Jan 26 '07 #7
"Damien" <no**************@recursor.netwrote in message
news:11*********************@k78g2000cwa.googlegro ups.com...
>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?
Force of habit, really...

1) Instantiate the object
2) Populate its properties
3) Add it to the collection
Jan 26 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: genc ymeri | last post by:
Hi, I have a C# from and I'm trying to add some code in onKeyDown event. But it doesn't fire at all............ Any idea ? Thanks a lot.
3
by: Brent Burkart | last post by:
I have a dropdownlist that won't fire .The autopostback is set to true and I do a ispostback check before population the ddl. Can anyone help please? Brent
2
by: Paul Lacey | last post by:
When dynamically placing dropdownlists inside a panel, the dropdownlist's SelectedIndexChanged event doesn't fire. If you take the dropdownlist out of the panel it works properly. Autopostback is...
1
by: Timo | last post by:
I am trying to use the DropDownList_SelectedIndexChanged event on a dropdown which is dynamically populated with different values at runtime, depending on what the user has been doing. The dropdown...
1
by: Jack | last post by:
Hello, I have a dropdown list on a user control and the AutoPostBack property is set to True. I want to use the SelectedIndexChanged event to populate some text boxes based on what the user...
6
by: Shimon Sim | last post by:
I have Panel control on the page. I am handling Init event for it. It doesn't seem to fire at all. Why? Thank you Shimon.
45
by: Pat | last post by:
its seems asp.net validation doesn't fire when using FireFox? Tested a page and it doesn't fire. It seems the javascript doesn't fire Any ideas?
3
by: amanbindal | last post by:
How to fire Dropdownlist selectedindexchanged event in JAVASCRIPT? Actually i want that on Dropdownlist selectedindexchanged event i got displayed data from a XML file.....!!
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.