I did follow the article TRULLY Understanding ViewState (great article btw) and populating my drop down list is working great. I've even setup a OnSelectedIndexChange event which fires almost as great.
The problem I've found is the SelectedIndexChanged event won't fire when selecting the 0th index. It does all other times however.
Here's some code:
<asp:DropDownList runat="server" ID="DropDownList1" EnableViewState="false"
AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" />
protected override void OnInit(EventArgs e)
{
this.DropDownList1.DataTextField = "Text";
this.DropDownList1.DataValueField = "Value";
this.DropDownList1.DataSource = fillQueueDropDown();
this.DropDownList1.DataBind();
base.OnInit(e);
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
OnSelectedQueueChanged(e);
}
public void OnSelectedQueueChanged(EventArgs e)
{
// Do stuff.
}
public event EventHandler queueNamesChangedEvent;
public void OnSelectedQueueChanged(EventArgs e)
{
if (queueNamesChangedEvent != null)
queueNamesChangedEvent(this, e);
}
I suppose I can do some type of check in the Page_Load method:
if(ViewState["selectedIndexChangedFlag"] != 1)
// raise OnSelectedChange event
Or is there something I can setup in the OnInit() method where I'm rebinding this data everytime that i can do?
See, my custom EventHander raises an event which is caught by a the parent page in which this control resides, so that the parent could take some action using the newly selected value. And this is currently working for all cases where the selected index > 0.
I create a property in this control which contains the most recently selected index, in which case my parent page can action on this property value on every Page_Load... dunno.
Open to suggestions. Or how to force this SelectedIndexChanged event to fire for that 0th index selection.