Link to home
Start Free TrialLog in
Avatar of smacca
smaccaFlag for Australia

asked on

DropDownList OnSelectedIndexChanged event never firing

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.
SOLUTION
Avatar of chmohan
chmohan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rodney Helsens
Rodney Helsens

Are you populating the DropDownList with a DataBind()?
If so, make sure you are checking is !Page.IsPostBack otherwise your going in circles.
Avatar of smacca

ASKER

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?
Avatar of smacca

ASKER

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.
Avatar of smacca

ASKER

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.