I've got the following DropDownList in my code that is firing in the wrong order:
public class MyWebpart : WebPart
{
private DropDownList dropDown = new DropDownList();
private string selectedValue;
public Webpart()
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
dropDown.AutoPostBack = true;
dropDown.SelectedIndexChanged += new EventHandler(DropDown_SelectedIndexChanged);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.EnsureChildControls();
}
protected void DropDown_SelectedIndexChanged(Object sender, EventArgs e)
{
selectedValue - dropDown.SelectedValue;
}
protected void override void CreateChildControls()
{
base.CreateChildControls();
// create some stuff here
}
I was expecting when the drop down selection changes, the DropDown_SelectedIndexChanged will get called first, but instead it went through the entire lifecycle going from OnInit, OnLoad, CreateChildControls, then DropDown_SelectedIndexChanged.
Am I missing something? How can I get DropDown_SelectedIndexChanged call first?
You can't change the page lifecycle. What you can do is either check if Page.IsPostBack and do something appropriate only on first load OR you can create a webservice and call that webservice from javascript to execute your selectedindexchanged actions in js rather than posting back the whole page.
Good luck!