deloused
asked on
Check if item is in data
I need to check if an item is in my datasource on post back. I am using C# with asp.net. What I have is a cascading dropdown, where the first dropdown's selected item(projects) determines the second dropdown(users). I would like to store the user and keep it "selected" if they exist in another project.
My psuedo code looks like this, but I'm not sure about syntax:
On Postback
string selectedusername = dropdown2.selecteditem;
if (selectedusername is in datasource items)
{
dropdown1.selecteditem = selectedusername
}
else
thanks in advance
My psuedo code looks like this, but I'm not sure about syntax:
On Postback
string selectedusername = dropdown2.selecteditem;
if (selectedusername is in datasource items)
{
dropdown1.selecteditem = selectedusername
}
else
thanks in advance
ASKER
I'm getting the error:
Cannot implicitly convert type 'System.Web.UI.WebControls .ListItem' to 'bool'
Why is it thinking it's a bool? Here's my code:
Cannot implicitly convert type 'System.Web.UI.WebControls
Why is it thinking it's a bool? Here's my code:
String PreviouslySelectedUser = UserList.SelectedValue;
if (UserList.Items.FindByValue(PreviouslySelectedUser))
{
UserList.DataBind();
UserList.SelectedValue = PreviouslySelectedUser;
}
else
{
UserList.DataBind();
//do some other stuff
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, I'm getting really close now but I'm getting an error.
'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list. What I need to check is something like this:
if (UserList.Items has(or contains) the item(PreviouslySelectedUse r))
{
UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
UserList.SelectedIndex = 0;
}
does that make sense? It's really close, but with the last code you gave me it's always iterating the if and never the else.
'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list. What I need to check is something like this:
if (UserList.Items has(or contains) the item(PreviouslySelectedUse
{
UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
UserList.SelectedIndex = 0;
}
does that make sense? It's really close, but with the last code you gave me it's always iterating the if and never the else.
ASKER
Ok, I'm getting really close now but I'm getting an error.
'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list. What I need to check is something like this:
if (UserList.Items has(or contains) the item(PreviouslySelectedUse r))
{
UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
UserList.SelectedIndex = 0;
}
does that make sense? It's really close, but with the last code you gave me it's always iterating the if and never the else.
'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list. What I need to check is something like this:
if (UserList.Items has(or contains) the item(PreviouslySelectedUse
{
UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
UserList.SelectedIndex = 0;
}
does that make sense? It's really close, but with the last code you gave me it's always iterating the if and never the else.
ASKER
Ok, I'm getting really close now but I'm getting an error.
'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list. What I need to check is something like this:
if (UserList.Items has(or contains) the item(PreviouslySelectedUse r))
{
UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
UserList.SelectedIndex = 0;
}
does that make sense? It's really close, but with the last code you gave me it's always iterating the if and never the else.
'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list. What I need to check is something like this:
if (UserList.Items has(or contains) the item(PreviouslySelectedUse
{
UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
UserList.SelectedIndex = 0;
}
does that make sense? It's really close, but with the last code you gave me it's always iterating the if and never the else.
ASKER
Ok, I'm getting really close now but I'm getting an error.
'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list. What I need to check is something like this:
if (UserList.Items has(or contains) the item(PreviouslySelectedUse r))
{
UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
UserList.SelectedIndex = 0;
}
does that make sense? It's really close, but with the last code you gave me it's always iterating the if and never the else.
'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list. What I need to check is something like this:
if (UserList.Items has(or contains) the item(PreviouslySelectedUse
{
UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
UserList.SelectedIndex = 0;
}
does that make sense? It's really close, but with the last code you gave me it's always iterating the if and never the else.
ASKER
Thanks for your help! I figured it out! I had something missing, your code worked prefectly!
Open in new window