Avoiding "SelectedValue which is invalid because it does not exist in the list of items"

D

Dave

Why doesn't this work?

My webpage changes databound items in a dropdown list depending on
what form the user selects. And the page needs to remember what the
user selected, and reselect the dropdown items between posts (using
selectedValue).

The problem is I get the above "does not exist" error when the user
switches forms because of the list changing.

So, I was trying to make a function search for the text in the list
and return a default value (or null) if necessary.

Here is the list:

<asp:dropdownlist SelectedValue='<%# rFolding() %>' ID="tFolding"
runat="Server" cssclass="formfield" datasourceid="ListFoldingSource"
DataTextField="Item" DataValueField="Item"/></td>

And the relevant function looks something like this:

String rFolding()
{
String rtn = ...read from database...
// Look for the string in the list
ListItem lstitem = tFolding.Items.FindByText(rtn);
if (lstitem == null) rtn = "(Select)"; // if not found,
"(Select)" is in every dropdown list so use it
return rtn;
}

This halfway works, but I'm still getting the error when the user
switches forms and the list items change. I expected this technique
to avoid ever getting: "SelectedValue which is invalid because it does
not exist in the list of items." Did I make some silly mistake or is
there an overall problem? Why doesn't this work?
 
N

nahid

Why doesn't this work?

My webpage changes databound items in a dropdown list depending on
what form the user selects. And the page needs to remember what the
user selected, and reselect the dropdown items between posts (using
selectedValue).

The problem is I get the above "does not exist" error when the user
switches forms because of the list changing.

So, I was trying to make a function search for the text in the list
and return a default value (or null) if necessary.

Here is the list:

<asp:dropdownlist SelectedValue='<%# rFolding() %>' ID="tFolding"
runat="Server" cssclass="formfield" datasourceid="ListFoldingSource"
DataTextField="Item" DataValueField="Item"/></td>

And the relevant function looks something like this:

String rFolding()
{
String rtn = ...read from database...
// Look for the string in the list
ListItem lstitem = tFolding.Items.FindByText(rtn);
if (lstitem == null) rtn = "(Select)"; // if not found,
"(Select)" is in every dropdown list so use it
return rtn;
}

This halfway works, but I'm still getting the error when the user
switches forms and the list items change. I expected this technique
to avoid ever getting: "SelectedValue which is invalid because it does
not exist in the list of items." Did I make some silly mistake or is
there an overall problem? Why doesn't this work?

hi,
please check you are binding the list after checking ispostback

if(!ispostback)
{
//bind the combo here
}

hope this help

nahid
http://nahidulkibria.blogspot.com/
http://kaz.com.bd/
 
D

Dave

hi,
please check you are binding the list after checking ispostback

if(!ispostback)
{
//bind the combo here

}

hope this help

nahidhttp://nahidulkibria.blogspot.com/http://kaz.com.bd/- Hide quoted text -

- Show quoted text -

Thanks! I was doing it all wrong. That way appears to work
beautifully in my situation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top