Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DropDownList Problem

Status
Not open for further replies.

James1981

Programmer
Nov 2, 2002
76
US
Hi, I'm trying to set the selected items in two different DropDownLists using the following code:

listSecurity_q1.Items.FindByText(aUser.security_question_1).Selected = true;

listSecurity_q2.Items.FindByText(aUser.security_question_2).Selected = true;

However, when i run the page I get a server error telling me that "A DropDownList cannot have multiple items selected." I'm a bit confused because I am setting the selected value for two different DropDownLists, and never am I trying to select Items.

Any ideas guys?

James
 
Hi James,

I remember i had some sort of problem like that before. I sorted out like calling a sub to deselect all the selections in listbox just before my new selection.

something like:

call DeselectAnySelection()
'this will loop and deselect if any, which simply loops and sets selected=false


'then call ur code ...
listSecurity_q2.Items.FindByText (aUser.security_question_2).Selected = true;

hope this helped,

raman
 
Raman,

Ok, using your idea, I tried calling clearselection() on both lists before I try to set the selection (I believe this would do the same as your loop idea) but I still get the same error. I also tried with your loop idea but still to no avail.

here is the full code for populating the lists with items, perhaps there is something wrong there also?

// Pass questions into drop down
while (mySecurity_reader.Read())
{
ListItem anItem = new ListItem();
anItem.Value = mySecurity_reader[0].ToString();
anItem.Text = mySecurity_reader[1].ToString();
listSecurity_q1.Items.Add(anItem);
listSecurity_q2.Items.Add(anItem);
}

// Add the headers
listSecurity_q1.Items.Insert(0, "-----------------------");
listSecurity_q1.Items.Insert(0, "Select Question");
listSecurity_q2.Items.Insert(0, "-----------------------");
listSecurity_q2.Items.Insert(0, "Select Question");

// Set the indices
for(int i=0;i<listSecurity_q1.Items.Count;i++)
{
listSecurity_q1.Items.Selected = false;
listSecurity_q2.Items.Selected = false;
}
listSecurity_q1.Items.FindByText(aUser.security_question_1).Selected = true;
listSecurity_q2.Items.FindByText(aUser.security_question_2).Selected = true;

// close reader
mySecurity_reader.Close();
 
Raman,

Ok, using your idea, I tried calling clearselection() on both lists before I try to set the selection (I believe this would do the same as your loop idea) but I still get the same error. I also tried with your loop idea but still to no avail.

here is the full code for populating the lists with items, perhaps there is something wrong there also?
Code:
// Pass questions into drop down
while (mySecurity_reader.Read())
{
 ListItem anItem = new ListItem();
 anItem.Value = mySecurity_reader[0].ToString();
 anItem.Text = mySecurity_reader[1].ToString();
 listSecurity_q1.Items.Add(anItem);
 listSecurity_q2.Items.Add(anItem);			
} 			
					
// Add the headers
listSecurity_q1.Items.Insert(0, &quot;-----------------------&quot;);
listSecurity_q1.Items.Insert(0, &quot;Select Question&quot;);
listSecurity_q2.Items.Insert(0, &quot;-----------------------&quot;);
listSecurity_q2.Items.Insert(0, &quot;Select Question&quot;);

// Set the indices	
for(int i=0;i<listSecurity_q1.Items.Count;i++)
{
 listSecurity_q1.Items[i].Selected = false;
 listSecurity_q2.Items[i].Selected = false;
}

listSecurity_q1.Items.FindByText(aUser.security_question_1).Selected = true;
			
listSecurity_q2.Items.FindByText(aUser.security_question_2).Selected = true;

							
// close reader
mySecurity_reader.Close();

Any ideas?
 
James,

I am not very sure what you are trying to do. And i am not a C# expert as well. But if you want to find a entry in a list box and select it you most probably can do it as bellow:

mystr=listSecurity_q1.Items.FindByText(aUser.security_question_1) 'i am not sure abt this and i am not in my windows xp to test it

listSecurity_q1.Items(0).text=mystr

hope i understand ur problem.

urs
 
Raman,

Not sure i follow you there, and i don't think you understand my problem at the moment. Basically, the selected item in the list must be set by a value from a DataReader. I'm not sure that the code above achieves anything.

Besides, the problem seems to be that the server thinks that the dropdownlists are in fact the same, which would explain why it is complaining that you cannot have two selected items in an list. This is so weird!!!!

Any one else experienced this before?

Cheers

James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top