Solved

DropDownList OnSelectedIndexChanged event never firing

Posted on 2004-08-13
7
9,273 Views
Last Modified: 2010-05-18
I have read through all the troubleshooting and other tickets posted on EE regarding this problem, however, it is still occuring with my code.
I have the following web form:

            <form name="myForm" id="myForm" method="post" RunAt="server">                                    
                  <asp:DropDownList ID="ddlType" OnSelectedIndexChanged="ddlType_SelectedIndexChanged" AutoPostBack="True" RunAt="server" />
            </form>



I have the following code:

            protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
            {
                  this.Redirect();
            }

            private void Redirect()
            {
                  //create url
                  string URL = "modifyAccomodation.aspx?";

                  if (null == searchTerm)
                        URL+= "?page=" + Convert.ToString(page-1);
                  else
                        URL+= "?page=" + Convert.ToString(page-1) + "&s=" + searchTerm;

                  //include ratings and types
                  if (type > 0)
                        URL += "&type=" + type;
                  if (rating > 0)
                        URL += "&rating=" + rating;
                  
                  //redirect
                  Response.Write(URL);
            }



The event NEVER fires!

Does the AutoPostBack attribute have to be set to True for event to fire?
Does it matter where you declare your databinding for the object?

I have also tried declaring event handler in the code as follows:

                  ddlType.SelectedIndexChanged +=new System.EventHandler(this.ddlType_SelectedIndexChanged);  


Really desperate to resolve this ASAP as I have to show client working page tomorrow.

Cheers.
0
Comment
Question by:smacca
  • 3
  • 3
7 Comments
 
LVL 1

Assisted Solution

by:chmohan
chmohan earned 200 total points
ID: 11793276
did you try to declare another event and see if it fires? like ddl click or so? also place an alert or so to see if u actually come to the first event before redirect
0
 
LVL 9

Expert Comment

by:Rodney Helsens
ID: 11793278
Are you populating the DropDownList with a DataBind()?
If so, make sure you are checking is !Page.IsPostBack otherwise your going in circles.
0
 

Author Comment

by:smacca
ID: 11793296
If I place the databind in a condition with !Page.IsPostBack, none of the list options are displayed.
Why is it that you have to do this?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:smacca
ID: 11793312
chmohan: I am debugging and simply setting a break point within the event handler so the redirect never comes into play. The event is not firing at all and thus I can never break into the code in the handler.
0
 

Author Comment

by:smacca
ID: 11793384
I removed the following statements from the top of ASPX page and event is working now:

EnableViewState="false" AutoEventWireup="false"

Can anyone explain why this is happening?
0
 
LVL 9

Accepted Solution

by:
Rodney Helsens earned 300 total points
ID: 11793394
The IsPostBack property returns a value indicating whether the page is being loaded in response to a client postback, or if it is being loaded and accessed for the first time.

You should only need to databind your dropdownlist once when the page is initially loaded.. on postback, you want to process the form, not necessarily rebuild the form controls from the database again..

Why it does not populate when you put !Page.IsPostBack is a result of your page design, can you post some more of the relevant code?
0
 
LVL 9

Expert Comment

by:Rodney Helsens
ID: 11793539
When set to true asp.net will wire up the methods of the page class with events, I believe this is more for VB guys than c#.

If you set AutoEventWireUp = false, which I believe is the default for c#, you need to assign event handlers manually, like you showed above, but also do it for the page load event.

What are you using for your IDE? VS.NET generates code like this when set to AutoEventWireUp=false;


            override protected void OnInit(EventArgs e) {
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            private void InitializeComponent() {
                  this.Load += new System.EventHandler(this.Page_Load);
            }

You would also need to add the dropdownlist event handler at this point.


0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now