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

Discussion in 'ASP .Net' started by Dave, Jun 30, 2007.

  1. Dave

    Dave Guest

    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?
     
    Dave, Jun 30, 2007
    #1
    1. Advertising

  2. Dave

    nahid Guest

    On Jun 30, 5:25 am, Dave <> 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/
     
    nahid, Jun 30, 2007
    #2
    1. Advertising

  3. Dave

    Dave Guest

    On Jun 29, 11:35 pm, nahid <> wrote:
    > On Jun 30, 5:25 am, Dave <> 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.
     
    Dave, Jun 30, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith R. Williams

    Re: Avoiding latches

    Keith R. Williams, Jul 14, 2003, in forum: VHDL
    Replies:
    0
    Views:
    1,197
    Keith R. Williams
    Jul 14, 2003
  2. Ken Smith

    Re: Avoiding latches

    Ken Smith, Jul 15, 2003, in forum: VHDL
    Replies:
    3
    Views:
    2,597
    Tim Hubberstey
    Jul 17, 2003
  3. far
    Replies:
    5
    Views:
    78,011
    antonacheradu
    Jan 14, 2012
  4. sree reddy
    Replies:
    2
    Views:
    1,768
    David C
    Jun 22, 2007
  5. Jason
    Replies:
    0
    Views:
    2,832
    Jason
    Aug 7, 2007
Loading...

Share This Page