Solved

DropDownList OnSelectedIndexChanged event never firing

Posted on 2004-08-13
7
9,294 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Video Player 11 23
Service Controller 1 23
Cant save 3D 4 20
Can I command line build a C# application? 6 33
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

772 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