Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9326
  • Last Modified:

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.
0
smacca
Asked:
smacca
  • 3
  • 3
2 Solutions
 
chmohanCommented:
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
 
Rodney HelsensCommented:
Are you populating the DropDownList with a DataBind()?
If so, make sure you are checking is !Page.IsPostBack otherwise your going in circles.
0
 
smaccaAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
smaccaAuthor Commented:
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
 
smaccaAuthor Commented:
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
 
Rodney HelsensCommented:
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
 
Rodney HelsensCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now