Search code examples
asp.netuser-controlsdrop-down-menuviewstateselectedindexchanged

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.


Solution

  • My goal with disabling the ViewState on this drop down list is to minimize the size of the ViewState for the page.

    The problem I had with only doing the if(!Page.IsPostBack){...DataBind()...}, is that when you select an item for the first time, and the page reloads, my drop down list becomes empty.

    What I ended up doing was creating another Property on this control, LastIndex. When the OnSelectedIndexChanged event fires, I update the LastIndex value. In the Page_Load, I compare the Current and Last index values, if they're different, then fire a Index changed event.

        public int SelectedValue{
            get { return this.DropDownList1.SelectedItem.Value; }
        }
    
        public int LastIndex{
            get { return this.ViewState["lastIndex"] == null ? -1 : (int)this.ViewState["lastIndex"]; }
            set { this.ViewState["lastIndex"] = value; }
        }
    
        protected override void OnInit(EventArgs e){
            base.OnInit(e);
            this.DropDownList1.DataTextField = "Text";
            this.DropDownList1.DataValueField = "Value";
            this.DropDownList1.DataSource = fillQueueDropDown();
            this.DropDownList1.DataBind();
        }
    
        protected void Page_Load(object sender, EventArgs e){
            if (this.LastIndex != this.SelectedValue)
                this.OnSelectedQueueChanged(new EventArgs());
        }
    
        private ListItemCollection fillQueueDropDown(){...}
    
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){
            OnSelectedQueueChanged(e);
            this.LastIndex = this.SelectedValue;
        }
    
        public event EventHandler queueNamesChangedEvent;
        public void OnSelectedQueueChanged(EventArgs e){
            if (queueNamesChangedEvent != null)
                queueNamesChangedEvent(this, e);
        }
    

    You are right though. The data is re-loaded and re-bound in the OnInit phase. Then the ViewState is restored (and when the 0th index is restored), when we finally get to the Events phase, the control doesn't detect the change.

    Not sure this is the most elegant route, but it's working good so far.

    Then i found this in the msdn docs for IPostBackDataHandler:

      public virtual bool LoadPostData(string postDataKey, 
         NameValueCollection postCollection) {
    
         String presentValue = Text;
         String postedValue = postCollection[postDataKey];
    
         if (presentValue == null || !presentValue.Equals(postedValue)) {
            Text = postedValue;
            return true;
         }
    
         return false;
      }
    

    Since the present value is the same as the changed-to value, the event isn't fired.