Link to home
Start Free TrialLog in
Avatar of smacca
smaccaFlag for Australia

asked on

The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive.

Hi,

I have a Custom DropDownList that is causing the following error:
 

                    The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive.


For some reason, this DropDownList works with some data and not others - I guess this would suggest the data is the problem (i have attached data results as well)
The DropDownList retrieves a datalist (name,value fields) from the database and binds them to the inherited control.
The inherited control is also a DropDownList extension that enables us to add empty fields to top or bottom of list.

Any help here would be appreciated. This has realy stumped me.

I have provided all code below:


--------------------------------------------------------------------------------------
using System;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using MWeb.Data;
using DKC.Data;

namespace DKC.Web.UI.CustomControls
{      
      ///<summary>
      /// Service Custom Dropdown List
      ///</summary>
      public class ServiceDropDownList :  MWeb.Web.UI.CustomControls.ExtraItemDropDownList
      {                                                      
            public override string DataTextField
            {
                  get
                  {                
                        return (base.DataTextField == string.Empty) ? "DataTextField" : base.DataTextField;
                  }
                  set
                  {                  
                        base.DataTextField = value;
                  }
            }

            public override string DataValueField
            {
                  get
                  {
                        return (base.DataValueField == string.Empty) ? "DataValueField" : base.DataValueField;
                  }
                  set
                  {
                        base.DataValueField = value;
                  }
            }

            protected override void Fill()
            {
                  try
                  {
                        this.DataSource = DataProviderFactory.GetServiceDataProvider().DataReaderProvider.GetList();

                        /************** THE DATA RETURNED FROM THE ABOVE CALL *********

                        2      Asset Management
                        1      Funds Management
                        3      Investment Opportunities

                        ****************/
                        this.DataBind(); /************ ERROR BUBBLES THROUGH HERE (see ExtraItemDropDownList as this overrides the DropDownList DataBind method)! ***********************/
                  }
                  catch ( Exception ex )
                  {
                        bool rethrow = ExceptionPolicy.HandleException( ex, "Data Policy" );
                        if(rethrow)
                        {
                              throw;
                        }
                  }
            }
      }
}


--------------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using System.Collections;
using System.ComponentModel;

namespace MWeb.Web.UI.CustomControls
{      
      /// <summary>
      /// Extra Item Position in DDL
      /// </summary>
      public enum ExtraItemPosition
      {
            Top,
            Bottom
      };

      /// <summary>
      /// Base Class for Extra Item <code>DropDownList</code>s
      /// </summary>
      public abstract class ExtraItemDropDownList : System.Web.UI.WebControls.DropDownList
      {      
            #region Constants

            public const string DEFAULT_EXTRAITEM_TEXT = "None";
            public const string DEFAULT_EXTRAITEM_VALUE = "0";

            public const string DEFAULT_DATATEXT_FIELD = "DataTextField";
            public const string DEFAULT_DATAVALUE_FIELD = "DataValueField";

            #endregion

            #region Constructor

            public ExtraItemDropDownList()
            {
            }

            #endregion

            #region Abstract and Virtuals

            protected abstract void Fill();
            protected virtual void Refresh()
            {
                  this.Fill();
            }

            #endregion

            #region Overrides

            ///<summary>
            ///Fills the DropDownList with items from the database on the first request
            ///If PopulateOnLoad is true. Will populate itself on any request where
            ///IsDirtyData is true.
            ///</summary>
            protected override void OnLoad(EventArgs e)
        {            
            base.OnLoad(e);
            if( (!Page.IsPostBack && this.PopulateOnLoad) || this.IsDirtyData)
            {
                this.Fill();
                this.IsDirtyData = false;
            }            
            }

            public override void DataBind()
            {
                  base.DataBind(); /************ ERROR HERE WHEN BINDING! ***********************/

                  //create new list item
                  if (this.IncludeExtraItem)
                  {
                        ListItem newItem = new ListItem();
                        newItem.Text = this.ExtraItemText;
                        newItem.Value = this.ExtraItemValue;
                        newItem.Selected = false;

                        //add list item to specified position
                        if((this.ExtraItemPosition == ExtraItemPosition.Top))
                        {
                              this.Items.Insert(0,newItem);
                        }
                        else if((ExtraItemPosition == ExtraItemPosition.Bottom))
                        {
                              this.Items.Add(newItem);
                        }
                  }

                  // now select the default item
                  if(this.InitialValue.ToString().Length > 0)
                  {
                        foreach (ListItem listItem in this.Items)
                        {
                              if(listItem.Value == InitialValue)
                              {
                                    listItem.Selected = true;
                                    break;
                              }
                        }
                  }
                  else
                  {
                        this.SelectedIndex = 0;
                  }
            }
            
            #endregion

            #region Properties

            [Description("If you set this value to true before the Load event occours then the control will reload its self."),Category("Loading")]
            public bool IsDirtyData
        {
            get
            {
                if(this.ViewState["IsDirtyData"] == null) this.ViewState["IsDirtyData"] = false;
                return (bool)this.ViewState["IsDirtyData"];
            }
            set
            {
                this.ViewState["IsDirtyData"] = value;
            }
        }            
            [Description("If you set this value to true then this control will pre-populate itself when it loads."),Category("Loading")]
        public bool PopulateOnLoad
        {
            get
            {
                if(this.ViewState["PopulateOnLoad"] == null) this.ViewState["PopulateOnLoad"] = true;
                return (bool)this.ViewState["PopulateOnLoad"];
            }
            set
            {
                this.ViewState["PopulateOnLoad"] = value;
            }
        }
            [Description("The value of the initially selected item"),Category("Data")]
            public string InitialValue
            {
                  get
                  {
                        if(this.ViewState["InitialValue"] == null)
                        {
                              this.ViewState["InitialValue"] = string.Empty;
                        }
                        return (string)this.ViewState["InitialValue"];
                  }

                  set
                  {
                        this.ViewState["InitialValue"] = value;
                  }
            }

            [Description("bool:  Include extra item in list?"),Category("Extra")]
            public bool IncludeExtraItem
            {
                  get
                  {
                        if(this.ViewState["IncludeExtraItem"]==null)
                        {
                              this.ViewState["IncludeExtraItem"] = false;
                        }
                        return (bool) this.ViewState["IncludeExtraItem"];
                  }
                  set
                  {
                        this.ViewState["IncludeExtraItem"] = value;
                  }
            }

            [Description("bool:  Should the extra item always been shown at the top of the list"),Category("Extra")]
            public ExtraItemPosition ExtraItemPosition
            {
                  get
                  {
                        if(this.ViewState["ExtraItemPosition"]==null)
                        {
                              this.ViewState["ExtraItemPosition"] = ExtraItemPosition.Top;
                        }
                        return (ExtraItemPosition) this.ViewState["ExtraItemPosition"];
                  }
                  set
                  {
                        this.ViewState["ExtraItemPosition"] = value;
                  }
            }

            [Description("string:  The text string displayed for extra item"),Category("Extra")]
            public string ExtraItemText
            {
                  get
                  {
                        if(this.ViewState["ExtraItemText"]==null)
                        {
                              this.ViewState["ExtraItemText"] = string.Empty;
                        }
                        return (string) this.ViewState["ExtraItemText"];
                  }
                  set
                  {
                        this.ViewState["ExtraItemText"] = value;
                  }
            }            

            [Description("string:  The value used for the extra item"),Category("Extra")]
            public string ExtraItemValue
            {
                  get
                  {
                        if(this.ViewState["ExtraItemText"]==null)
                        {
                              this.ViewState["ExtraItemText"] = string.Empty;
                        }
                        return (string) this.ViewState["ExtraItemText"];
                  }
                  set
                  {
                        this.ViewState["ExtraItemText"] = value;
                  }
            }      
      
            #endregion

      }
}

--------------------------------------------------------------------------------------




ASKER CERTIFIED SOLUTION
Avatar of AGBrown
AGBrown
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smacca

ASKER

Your default values for the extra item text and value are None and 0 respectively. Are you using these for this particular drop downlist
- Does this matter?

-Is every list throwing the error, or just a particular one?
Only certain lists are throwing error. I have provided the data for the list that is throwing the error (see commented *** code).

-Does the error occur on the base.DataBind() line, or one of the other lines in the ExtraItemDropDownList.DataBind method?
It is always happening at the base.DataBind() line!!