Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-04-12
2
Medium Priority
?
2,494 Views
Last Modified: 2008-01-09
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

      }
}

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




0
Comment
Question by:smacca
2 Comments
 
LVL 12

Accepted Solution

by:
AGBrown earned 1000 total points
ID: 16447211
-Your default values for the extra item text and value are None and 0 respectively. Are you using these for this particular drop downlist
-Is every list throwing the error, or just a particular one?
-Does the error occur on the base.DataBind() line, or one of the other lines in the ExtraItemDropDownList.DataBind method?
0
 

Author Comment

by:smacca
ID: 16474080
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!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question