This is an old post but I'll reply for other people that reference it, as it is how I stumbled upon it.
I think I know what your problem is here...
Although you can set the .SelectedIndex and .SelectedValue properties in Page_Load() it does not get set there when the data is not bound. It only gets set after DataBind() has been called (In your case probably by a SqlDataSource). So in your case you would have to call ddlCustomer_SelectedIndexChanged(Nothing, Nothing) in the ddlCustomer_DataBound() event handler (DropDownList.DataBound).
Here is the code listing of a new component I derived from a DropDownList to get rid of the ugly: "'controlname' has a SelectedValue which is invalid because it does not exist in the list of items." error when assigning a value that's not in the list. The idea here is that it should go back to the "unassigned" item and not throw an exception.
New properties:
- bool AddUnassignedLine - set to true when you always want the unassigned ("please select") line.
- bool SelectUnassignedLineWhenInvalid - Add and/or select the unassigned line when the abovementioned error occurs.
- string UnassignedLineText - text for the unassigned line, default = "--Please Select--"
- string UnassignedLineValue - value for the unassigned line, default = "-1"
I'm still busy testing it, so I might post updates later.
This is my first ASP.NET/.NET/C# component so it might not be perfect. I've only been working with VS2005 for a week now. (coming from Delphi 7)
For those of you who don't know, here's the steps to create this custom control:
- In VS2005 go File->New->Project and select the Class Library
- Type Name and Solution Name at the bottom, click OK.
- then to add my stuff: copy all my code and past it over the created .cs file contents
- change the namespace if you like
- also change "CSEDropDownList" to what you like: [ToolboxData( "<{0}:CSEDropDownList runat=server></{0}:CSEDropDownList>" )]
public class CSEDropDownList: System.Web.UI.WebControls.DropDownList - Also add this to the bottom your AssemblyInfo.cs file: [assembly: TagPrefix( "CSEControls", "ddl" )]
- AFTER YOU BUILT THE COMP: To register on the Toolbox, right click the toolbox and select Choose Items..., select your new control there, if it's not there, browse for the .dll of your assembly.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CSEControls
{
[ToolboxData( "<{0}:CSEDropDownList runat=server></{0}:CSEDropDownList>" )]
public class CSEDropDownList: System.Web.UI.WebControls.DropDownList
{
private const string constUnassignedLineTextDefault = "--Please Select--";
private const string constUnAssignedLineValueDefault = "-1";
private const bool constAddUnassignedLineDefault = false;
[Bindable( true )]
[Category( "Behavior" )]
[DefaultValue( constUnassignedLineTextDefault )]
[Localizable( true )]
public string UnassignedLineText
{
get
{
String s = (String)ViewState["UnAssignedLineText"];
return ( ( s == null ) ? constUnassignedLineTextDefault : s );
}
set
{
ViewState["UnassignedLineText"] = value;
}
}
[Bindable( true )]
[Category( "Behavior" )]
[DefaultValue( constUnAssignedLineValueDefault )]
[Localizable( true )]
public string UnassignedLineValue
{
get
{
String s = (String)ViewState["UnassignedLineValue"];
return ( ( s == null ) ? constUnAssignedLineValueDefault : s );
}
set
{
ViewState["UnassignedLineValue"] = value;
}
}
[Bindable( true )]
[Category( "Behavior" )]
[DefaultValue( constAddUnassignedLineDefault )]
[Localizable( true )]
public bool AddUnassignedLine
{
get
{
return (bool)( ViewState["AddUnassignedLine"] ?? constAddUnassignedLineDefault );
}
set
{
ViewState["AddUnassignedLine"] = value;
}
}
[Bindable( true )]
[Category( "Behavior" )]
[DefaultValue( false )]
[Localizable( true )]
public bool SelectUnassignedLineWhenInvalid
{
get
{
return (bool)( ViewState["SelectUnassignedLineWhenInvalid"] ?? false );
}
set
{
ViewState["SelectUnassignedLineWhenInvalid"] = value;
}
}
public override string SelectedValue
{
get
{
return base.SelectedValue;
}
set
{
try
{
base.SelectedValue = value;
}
catch ( ArgumentOutOfRangeException ex )
{
if ( this.SelectUnassignedLineWhenInvalid )
{
CheckCreateUnassignedLine( true, true );
base.SelectedValue = this.UnassignedLineValue;
this.DataBind();
}
else
throw ex;
}
}
}
protected override void OnDataBound( EventArgs e )
{
base.OnDataBound( e );
}
protected override void OnDataBinding( EventArgs e )
{
try
{
base.OnDataBinding( e );
CheckCreateUnassignedLine( false, false );
}
catch ( ArgumentOutOfRangeException ex )
{
if ( this.SelectUnassignedLineWhenInvalid )
{
CheckCreateUnassignedLine( true, true );
this.SelectedValue = this.UnassignedLineValue;
}
else
throw ex;
}
}
private void CheckCreateUnassignedLine( bool ForceCreation, bool ForceSelection )
{
if ( ( this.AddUnassignedLine ) || ( ForceCreation ) )
{
if ( ForceSelection )
this.ClearSelection();
ListItem li;
bool bFound = false;
// Check if it does not exists already
li = this.Items.FindByValue( this.UnassignedLineValue );
if ( li != null )
if ( li.Text.Equals( this.UnassignedLineText ) )
bFound = true;
// Create only if not found
if ( !bFound )
{
li = new ListItem( this.UnassignedLineText, this.UnassignedLineValue );
this.Items.Insert( 0, li );
}
// Select if required
if ( ForceSelection )
li.Selected = true;
}
}
protected override void RenderContents( HtmlTextWriter output )
{
base.RenderContents( output );
output.Write( UnassignedLineText );
output.Write( UnassignedLineValue );
output.Write( AddUnassignedLine );
output.Write( SelectUnassignedLineWhenInvalid );
}
}
}