Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropDownList OnSelectedIndexChange to 0th index w/out ViewState

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.

like image 567
Dave Baghdanov Avatar asked Jan 23 '09 20:01

Dave Baghdanov


1 Answers

The problem is that you are loading the data each time and this is resetting the selected index. Imagine this is your dropdown:

zero [selected]
one
two

Then in the client you change the selected index:

zero
one [selected]
two

This populates the hidden input __EVENTARGUMENT with your new index (1) and the hidden input __EVENTTARGET with the id of your dropdown. Now the server-side code kicks in and reloads your data:

zero [selected]
one
two

"zero" is the selected value because that is the default when the data is loaded. Then ASP.NET looks for __EVENTTARGET and __EVENTARGUMENT in the Request and finds your dropdown's id and finds the new index (1). Now your dropdown looks like this:

zero 
one [selected]
two

Since the index has changed, the dropdown raises its SelectedIndexChanged event indicating that the index has changed. Obviously this is the part that is working, now lets see why selecting the first item in the list does not raise the event.

Now lets say that we still have the dropdown in the state it was just in (with "one" being selected and the selected index of 1). What happens when we select the first item in the list on the client?

__EVENTTARGET and __EVENTARGUMENT are populated with the id of the dropdown and the new index (0). Then the server loads the data into the dropdown and the dropdown now looks like this again:

zero [selected]
one
two

Notice that since you reloaded the data before the events fired the index is already set to 0 because that is the default. Now when your event fires and the dropdown's selected index is set to 0, the dropdown does not see this as a change since the selected index (as far as it knows) has not changed.

Here is how to fix the problem:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    if (!Page.IsPostBack)
    {
        this.DropDownList1.DataTextField = "Text";
        this.DropDownList1.DataValueField = "Value";
        this.DropDownList1.DataSource = fillQueueDropDown();
        this.DropDownList1.DataBind();
    }    
}

What this will do is only load the data into the dropdown if the page is not a postback. This means that ViewState will maintain the data for you as well as the selected index so that when you post back the dropdown will compare the new index to the index you saw in the client.

like image 114
Andrew Hare Avatar answered Oct 09 '22 01:10

Andrew Hare