smacca
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.Enterp riseLibrar y.Common;
using Microsoft.Practices.Enterp riseLibrar y.Data;
using Microsoft.Practices.Enterp riseLibrar y.Exceptio nHandling;
using Microsoft.Practices.Enterp riseLibrar y.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 .ExtraItem DropDownLi st
{
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.GetSer viceDataPr ovider().D ataReaderP rovider.Ge tList();
/************** 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.HandleExce ption( 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. DropDownLi st
{
#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,newIte m);
}
else if((ExtraItemPosition == ExtraItemPosition.Bottom))
{
this.Items.Add(newItem);
}
}
// now select the default item
if(this.InitialValue.ToStr ing().Leng th > 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["IsDirty Data"] == null) this.ViewState["IsDirtyDat a"] = false;
return (bool)this.ViewState["IsDi rtyData"];
}
set
{
this.ViewState["IsDirtyDat a"] = 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["Populat eOnLoad"] == null) this.ViewState["PopulateOn Load"] = true;
return (bool)this.ViewState["Popu lateOnLoad "];
}
set
{
this.ViewState["PopulateOn Load"] = value;
}
}
[Description("The value of the initially selected item"),Category("Data")]
public string InitialValue
{
get
{
if(this.ViewState["Initial Value"] == null)
{
this.ViewState["InitialVal ue"] = string.Empty;
}
return (string)this.ViewState["In itialValue "];
}
set
{
this.ViewState["InitialVal ue"] = value;
}
}
[Description("bool: Include extra item in list?"),Category("Extra")]
public bool IncludeExtraItem
{
get
{
if(this.ViewState["Include ExtraItem" ]==null)
{
this.ViewState["IncludeExt raItem"] = false;
}
return (bool) this.ViewState["IncludeExt raItem"];
}
set
{
this.ViewState["IncludeExt raItem"] = 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["ExtraIt emPosition "]==null)
{
this.ViewState["ExtraItemP osition"] = ExtraItemPosition.Top;
}
return (ExtraItemPosition) this.ViewState["ExtraItemP osition"];
}
set
{
this.ViewState["ExtraItemP osition"] = value;
}
}
[Description("string: The text string displayed for extra item"),Category("Extra")]
public string ExtraItemText
{
get
{
if(this.ViewState["ExtraIt emText"]== null)
{
this.ViewState["ExtraItemT ext"] = string.Empty;
}
return (string) this.ViewState["ExtraItemT ext"];
}
set
{
this.ViewState["ExtraItemT ext"] = value;
}
}
[Description("string: The value used for the extra item"),Category("Extra")]
public string ExtraItemValue
{
get
{
if(this.ViewState["ExtraIt emText"]== null)
{
this.ViewState["ExtraItemT ext"] = string.Empty;
}
return (string) this.ViewState["ExtraItemT ext"];
}
set
{
this.ViewState["ExtraItemT ext"] = value;
}
}
#endregion
}
}
-------------------------- ---------- ---------- ---------- ---------- ---------- ----------
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.Enterp
using Microsoft.Practices.Enterp
using Microsoft.Practices.Enterp
using Microsoft.Practices.Enterp
using MWeb.Data;
using DKC.Data;
namespace DKC.Web.UI.CustomControls
{
///<summary>
/// Service Custom Dropdown List
///</summary>
public class ServiceDropDownList : MWeb.Web.UI.CustomControls
{
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.GetSer
/************** 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.HandleExce
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.
{
#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
{
this.Items.Insert(0,newIte
}
else if((ExtraItemPosition == ExtraItemPosition.Bottom))
{
this.Items.Add(newItem);
}
}
// now select the default item
if(this.InitialValue.ToStr
{
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["IsDirty
return (bool)this.ViewState["IsDi
}
set
{
this.ViewState["IsDirtyDat
}
}
[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["Populat
return (bool)this.ViewState["Popu
}
set
{
this.ViewState["PopulateOn
}
}
[Description("The value of the initially selected item"),Category("Data")]
public string InitialValue
{
get
{
if(this.ViewState["Initial
{
this.ViewState["InitialVal
}
return (string)this.ViewState["In
}
set
{
this.ViewState["InitialVal
}
}
[Description("bool: Include extra item in list?"),Category("Extra")]
public bool IncludeExtraItem
{
get
{
if(this.ViewState["Include
{
this.ViewState["IncludeExt
}
return (bool) this.ViewState["IncludeExt
}
set
{
this.ViewState["IncludeExt
}
}
[Description("bool: Should the extra item always been shown at the top of the list"),Category("Extra")]
public ExtraItemPosition ExtraItemPosition
{
get
{
if(this.ViewState["ExtraIt
{
this.ViewState["ExtraItemP
}
return (ExtraItemPosition) this.ViewState["ExtraItemP
}
set
{
this.ViewState["ExtraItemP
}
}
[Description("string: The text string displayed for extra item"),Category("Extra")]
public string ExtraItemText
{
get
{
if(this.ViewState["ExtraIt
{
this.ViewState["ExtraItemT
}
return (string) this.ViewState["ExtraItemT
}
set
{
this.ViewState["ExtraItemT
}
}
[Description("string: The value used for the extra item"),Category("Extra")]
public string ExtraItemValue
{
get
{
if(this.ViewState["ExtraIt
{
this.ViewState["ExtraItemT
}
return (string) this.ViewState["ExtraItemT
}
set
{
this.ViewState["ExtraItemT
}
}
#endregion
}
}
--------------------------
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
- 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.Data
It is always happening at the base.DataBind() line!!