Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
DropDownList.SelectedIndex Issue
Message
From
30/06/2005 17:48:33
 
 
To
All
General information
Forum:
ASP.NET
Category:
Other
Title:
DropDownList.SelectedIndex Issue
Environment versions
Environment:
C# 1.1
OS:
Windows 2000 SP4
Database:
MS SQL Server
Miscellaneous
Thread ID:
01027923
Message ID:
01027923
Views:
51
This message has been marked as the solution to the initial question of the thread.
VS.NET 2003

I have a form with 3 DropDownList controls. First, I populate the controls from a DataTable pulled from SQL Server, then add a couple of other items to the top of each ddl. All of the ddls have the same ListItems to be chosen from (although their selections may be different). Loading in the choices works fine:
DataTable dt1 = ds.Tables["Domains1"];

if ( dt1.Rows.Count > 0 )
{
	// DataBind the ddls:
	ddlR1D1.DataSource = dt1;
	ddlR1D1.DataTextField = "dom_desc";
	ddlR1D1.DataValueField = "dom_pkey";
	ddlR1D1.DataBind( );

	ddlR1D2.DataSource = dt1;
	ddlR1D2.DataTextField = "dom_desc";
	ddlR1D2.DataValueField = "dom_pkey";
	ddlR1D2.DataBind( );

	ddlR1D3.DataSource = dt1;
	ddlR1D3.DataTextField = "dom_desc";
	ddlR1D3.DataValueField = "dom_pkey";
	ddlR1D3.DataBind( );
}
else
{
}
// Add the always-available ddl items:
ListItem li0 = new ListItem( "-- Not Selected --", "0" );
ListItem li1 = new ListItem( "None", "81" );

ddlR1D1.Items.Insert( 0, li0 );
ddlR1D1.Items.Insert( 1, li1 );
ddlR1D2.Items.Insert( 0, li0 );
ddlR1D2.Items.Insert( 1, li1 );
ddlR1D3.Items.Insert( 0, li0 );
ddlR1D3.Items.Insert( 1, li1 );
Now I want to set .SelectedIndex of each ddl based on values I've pulled up from a different SQL table:
// Select the proper Item in each ddl:
ddlSetSelectedIndex( ddlR1D1, dt.Rows[0]["ind_rater1_domain1"] );
ddlSetSelectedIndex( ddlR1D2, dt.Rows[0]["ind_rater1_domain2"] );
ddlSetSelectedIndex( ddlR1D3, dt.Rows[0]["ind_rater1_domain3"] );

private void ddlSetSelectedIndex( DropDownList ddl, object keyValue )
{
	if ( keyValue == DBNull.Value )
	{
		ddl.SelectedIndex = 0;
	}
	else
	{
		ddl.SelectedIndex = 
			ddl.Items.IndexOf( ddl.Items.FindByValue( keyValue.ToString( ) ) );
	}
}
However, this fails with an unhandled exception "A DropDownList cannot have multiple items selected."

I've confirmed that the item being sought is in the ddl's list (by value), I've traced the code in the debugger and watched .SelectedIndex being set properly in ddlSetSelectedIndex() but I can't get past this error.

- After the 3 ddlSetSelectedIndex calls, each .SelectedIndex value is unchanged (top item/unchanged) despite me watching it being set in the debugger.

- I've tried passing the ddls by reference i.e. ddlSetSelectedIndex( ref DropDownList ddl, ...) and updating the calls as well but that doesn't help either (besides it shouldn't be necessary to pass a ddl by reference, should it?)

- I've tried eliminating the function call by inlining the code in the calling routine

- I've tried putting ddl.ClearSelection( ) as the first line in ddlSetSelectedIndex()

None of the above makes any difference.

Can anyone offer any insight?

** UPDATE **

Found the problem. Turns out it was in the "Add the always-available ddl items:" section. The way it's done above is "efficient" in that li0 and li1 are defined once, then added to all 3 ddls. However, it appears what that code actually does is to add references to the same 2 ListItems to each ddl, rather than adding separate ListItems to each ddl. I wrote some very simple test code that set .SelectedIndex to hard-coded integer values and couldn't get even that to work.

The answer seems to be to make sure each ddl has new ListItems:
ddlR1D1.Items.Insert( 0, new ListItem( "-- Not Selected --", "0" ) );
ddlR1D1.Items.Insert( 1, new ListItem( "None", "81" ) );
ddlR1D2.Items.Insert( 0, new ListItem( "-- Not Selected --", "0" ) );
ddlR1D2.Items.Insert( 1, new ListItem( "None", "81" ) );
ddlR1D3.Items.Insert( 0, new ListItem( "-- Not Selected --", "0" ) );
ddlR1D3.Items.Insert( 1, new ListItem( "None", "81" ) );
This was a PITA to debug; I hope it helps someone else some day.
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Reply
Map
View

Click here to load this message in the networking platform