smacca
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="dd lType_Sele ctedIndexC hanged" AutoPostBack="True" RunAt="server" />
</form>
I have the following code:
protected void ddlType_SelectedIndexChang ed(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.SelectedIndexChang ed +=new System.EventHandler(this.d dlType_Sel ectedIndex Changed);
Really desperate to resolve this ASAP as I have to show client working page tomorrow.
Cheers.
I have the following web form:
<form name="myForm" id="myForm" method="post" RunAt="server">
<asp:DropDownList ID="ddlType" OnSelectedIndexChanged="dd
</form>
I have the following code:
protected void ddlType_SelectedIndexChang
{
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.SelectedIndexChang
Really desperate to resolve this ASAP as I have to show client working page tomorrow.
Cheers.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
Why is it that you have to do this?
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.
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?
EnableViewState="false" AutoEventWireup="false"
Can anyone explain why this is happening?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.P age_Load);
}
You would also need to add the dropdownlist event handler at this point.
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.P
}
You would also need to add the dropdownlist event handler at this point.
If so, make sure you are checking is !Page.IsPostBack otherwise your going in circles.