Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Avoiding "SelectedValue which is invalid because it does not exist in the list of items"

Reply
Thread Tools

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

 
 
Dave
Guest
Posts: n/a
 
      06-29-2007
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?

 
Reply With Quote
 
 
 
 
nahid
Guest
Posts: n/a
 
      06-30-2007
On Jun 30, 5:25 am, Dave <(E-Mail Removed)> wrote:
> 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/


 
Reply With Quote
 
 
 
 
Dave
Guest
Posts: n/a
 
      06-30-2007
On Jun 29, 11:35 pm, nahid <(E-Mail Removed)> wrote:
> On Jun 30, 5:25 am, Dave <(E-Mail Removed)> wrote:
>
>
>
>
>
> > 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
>
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SelectedValue which is invalid because it does not exist in the list of items. far ASP .Net 5 01-14-2012 05:47 PM
Any good solutions for: SelectedValue which is invalid because it does not exist in the list of items? randy.buchholz ASP .Net 4 07-16-2008 03:45 PM
System.ArgumentOutOfRangeException: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items. J055 ASP .Net 4 11-14-2007 03:08 AM
dropdownlist has a selectedvalue which is invalid because it does not exist in the list of items. Jason ASP .Net 0 08-07-2007 07:35 PM
'dropdownlist' has a selectedvalue which is invalid because it does not exist in the list of items.p sree reddy ASP .Net 2 06-22-2007 02:15 PM



Advertisments