Professional Web Applications Themes

Setting 'selected' item in bound drop down list - ASP.NET

You must set the selected index - meaning, you must know the location of the item that needs to be selected in the list. "ctb" <clarkspeakeasyinteractive.com> wrote in message news:020201c34d4c$ab83f130$a101280aphx.gbl... > I have a drop data bound drop down list. > When retrieving value froma database, I wish to set the > selected value for a particular item on page load after > the page is loaded. > > ideas on how to do this?...

  1. #1

    Default Re: Setting 'selected' item in bound drop down list

    You must set the selected index - meaning, you must know the location of
    the item that needs to be selected in the list.

    "ctb" <clarkspeakeasyinteractive.com> wrote in message
    news:020201c34d4c$ab83f130$a101280aphx.gbl...
    > I have a drop data bound drop down list.
    > When retrieving value froma database, I wish to set the
    > selected value for a particular item on page load after
    > the page is loaded.
    >
    > ideas on how to do this?

    Marina Guest

  2. #2

    Default Re: Setting 'selected' item in bound drop down list

    "Marina" <zlatkinamnospam.hotmail.com> wrote in message
    news:ugcW47UTDHA.1552TK2MSFTNGP10.phx.gbl...
    > You must set the selected index - meaning, you must know the location of
    > the item that needs to be selected in the list.
    >
    Not true. See my previous post.

    Paul


    paul crowder Guest

  3. #3

    Default Re: Setting 'selected' item in bound drop down list

    If there was already an item selected, this will throw an exception. If
    people get used to setting the Selected property of the item, they will
    forget to check whether or not something was already selected.

    Setting the SelectedIndex property however, is completely safe.

    "paul crowder" <paul.crowderacstechnologies.com> wrote in message
    news:Od%23WoEVTDHA.2480tk2msftngp13.phx.gbl...
    > "Marina" <zlatkinamnospam.hotmail.com> wrote in message
    > news:ugcW47UTDHA.1552TK2MSFTNGP10.phx.gbl...
    > > You must set the selected index - meaning, you must know the location
    of
    > > the item that needs to be selected in the list.
    > >
    >
    > Not true. See my previous post.
    >
    > Paul
    >
    >

    Marina Guest

  4. #4

    Default Re: Setting 'selected' item in bound drop down list

    "Marina" <zlatkinamnospam.hotmail.com> wrote in message
    news:%23GADNKVTDHA.2148TK2MSFTNGP10.phx.gbl...
    > If there was already an item selected, this will throw an exception. If
    > people get used to setting the Selected property of the item, they will
    > forget to check whether or not something was already selected.
    >
    > Setting the SelectedIndex property however, is completely safe.
    >
    I do agree with that, but using the SelectedIndex property has its drawbacks
    as well. You've still got to find the index of the item with the value
    you're looking for, so you'd either have to do a FindByValue on that, or
    loop through all the items. If you just get in the habit of doing this:

    ddl.Clelection();
    ddl.Items.FindByValue("somevalue").Selected = true;

    You should be okay.

    Paul


    paul crowder Guest

  5. #5

    Default Re: Setting 'selected' item in bound drop down list

    That code is still problematic. What if you are looking for a value that is
    not there? Then you get a nullreference exception.

    I would first check to make sure that call actually returned an object.

    Yes, most of the time the values we are looking for are there. But it is so
    easy to get in big trouble with this kind of thing if you are not careful.

    "paul crowder" <paul.crowderacstechnologies.com> wrote in message
    news:OymHswVTDHA.2148TK2MSFTNGP12.phx.gbl...
    > "Marina" <zlatkinamnospam.hotmail.com> wrote in message
    > news:%23GADNKVTDHA.2148TK2MSFTNGP10.phx.gbl...
    > > If there was already an item selected, this will throw an exception. If
    > > people get used to setting the Selected property of the item, they will
    > > forget to check whether or not something was already selected.
    > >
    > > Setting the SelectedIndex property however, is completely safe.
    > >
    >
    > I do agree with that, but using the SelectedIndex property has its
    drawbacks
    > as well. You've still got to find the index of the item with the value
    > you're looking for, so you'd either have to do a FindByValue on that, or
    > loop through all the items. If you just get in the habit of doing this:
    >
    > ddl.Clelection();
    > ddl.Items.FindByValue("somevalue").Selected = true;
    >
    > You should be okay.
    >
    > Paul
    >
    >

    Marina Guest

  6. #6

    Default Re: Setting 'selected' item in bound drop down list


    Marina is right , we actually just went through tthis problem in our
    project at work. It seems that if you data bind a dropdown list to a
    datatable , the full size of the fields are returned. for example if
    you have a VarChar(10) field defined and there is a value that is
    "ABC" tht you are trying to match, the field is displayed in the
    dropdown list as "ABC ". there are blank padded spaces. the
    findbyText and findbyValue methods fail. our web pages were bombing
    out on load because of this, so we wrote a wrapper that covers it. we
    pass in the dropdownList collection and the string we are trying to
    match adn trimm them both and use a string.compare to find the list
    index. If a match is foundwe retun the ListIndex of the match and set
    the dropdown selected List index to that , if no match is found we
    return -1 and set the ListIndex to that.
    this problem will occur if you assign a value from a list to an object
    and then that value in the list gets deleted or changed and you load
    the object and try to match the text or value from the list.
    Our wrapper seems to have this covered. We use one method for
    FindbyText and another for FindbyValue.
    I can post it on Monday if anyone wants it
    Nick P.
    MCSD

    On Fri, 18 Jul 2003 14:56:51 -0400, "Marina"
    <zlatkinamnospam.hotmail.com> wrote:

    That code is still problematic. What if you are looking for a value
    that is
    not there? Then you get a nullreference exception.

    I would first check to make sure that call actually returned an
    object.

    Yes, most of the time the values we are looking for are there. But it
    is so
    easy to get in big trouble with this kind of thing if you are not
    careful.

    "paul crowder" <paul.crowderacstechnologies.com> wrote in message
    news:OymHswVTDHA.2148TK2MSFTNGP12.phx.gbl...
    > "Marina" <zlatkinamnospam.hotmail.com> wrote in message
    > news:%23GADNKVTDHA.2148TK2MSFTNGP10.phx.gbl...
    > > If there was already an item selected, this will throw an exception. If
    > > people get used to setting the Selected property of the item, they will
    > > forget to check whether or not something was already selected.
    > >
    > > Setting the SelectedIndex property however, is completely safe.
    > >
    >
    > I do agree with that, but using the SelectedIndex property has its
    drawbacks
    > as well. You've still got to find the index of the item with the value
    > you're looking for, so you'd either have to do a FindByValue on that, or
    > loop through all the items. If you just get in the habit of doing this:
    >
    > ddl.Clelection();
    > ddl.Items.FindByValue("somevalue").Selected = true;
    >
    > You should be okay.
    >
    > Paul
    >
    >

    Nick Guest

Similar Threads

  1. How To Dynamically Set Selected Item in SELECT List inDW Object
    By dataczarina in forum Adobe Dreamweaver & Contribute
    Replies: 0
    Last Post: February 28th, 04:08 AM
  2. Dynamically setting selected item in control derived from DropDownList
    By Mario Vargas in forum ASP.NET Building Controls
    Replies: 1
    Last Post: July 24th, 10:04 PM
  3. Web User Control Drop Down List does not show selected item
    By stephg in forum ASP.NET Building Controls
    Replies: 3
    Last Post: October 30th, 01:00 PM
  4. Submitting selected List/Menu Item??
    By man jackals in forum Macromedia ColdFusion
    Replies: 4
    Last Post: March 24th, 05:18 PM
  5. How to set selected item in drop-down list in datagrid
    By Jos in forum ASP.NET Building Controls
    Replies: 0
    Last Post: August 20th, 07:46 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139