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

Discussion in 'ASP .Net' started by Marina, Jul 18, 2003.

  1. Marina

    Marina Guest

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

    "ctb" <> wrote in message
    news:020201c34d4c$ab83f130$...
    > 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, Jul 18, 2003
    #1
    1. Advertising

  2. Marina

    paul crowder Guest

    "Marina" <> wrote in message
    news:...
    > 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, Jul 18, 2003
    #2
    1. Advertising

  3. Marina

    Marina Guest

    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" <> wrote in message
    news:Od%...
    > "Marina" <> wrote in message
    > news:...
    > > 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, Jul 18, 2003
    #3
  4. Marina

    paul crowder Guest

    "Marina" <> wrote in message
    news:%...
    > 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.ClearSelection();
    ddl.Items.FindByValue("somevalue").Selected = true;

    You should be okay.

    Paul
    paul crowder, Jul 18, 2003
    #4
  5. Marina

    Marina Guest

    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" <> wrote in message
    news:...
    > "Marina" <> wrote in message
    > news:%...
    > > 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.ClearSelection();
    > ddl.Items.FindByValue("somevalue").Selected = true;
    >
    > You should be okay.
    >
    > Paul
    >
    >
    Marina, Jul 18, 2003
    #5
  6. Marina

    Nick Guest

    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"
    <> 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" <> wrote in message
    news:...
    > "Marina" <> wrote in message
    > news:%...
    > > 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.ClearSelection();
    > ddl.Items.FindByValue("somevalue").Selected = true;
    >
    > You should be okay.
    >
    > Paul
    >
    >
    Nick, Jul 19, 2003
    #6
  7. Marina

    KROSS

    Joined:
    Jan 21, 2008
    Messages:
    1
    Drop Down List list item not selecting the change

    Hey everyone. I am hoping you can help..

    I have a drop down list that I use a databind using a list collection,
    However it populates the data I want, but when a selection is made
    the change event is fired but the selected index is still the inital index

    Here is the code on page load that I use that populates the dropdown. on selection of the drop down it then populates a Gridview..it works fine if you are admin, but when the selection needs to be populated by the list item it makes the selection the change event is fired but it then keeps the initial value.. any help would be appreciated as I am new to the list item thing
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ddLOC.SelectedValue = Request.QueryString("loc")

    If Not (Page.IsPostBack) Then


    If Not Session("User") Is Nothing Then
    emp = CType(Session("User"), Employee)

    If Not emp.IsValidated Then
    Response.Redirect("~/perfAppraisal.aspx", True)
    End If

    Else
    Response.Redirect("~/perfAppraisal.aspx", True)
    End If

    ddLOC.DataSource = Location
    ddLOC.DataTextField = "Loc_Name"
    ddLOC.DataValueField = "LOC_NBR"
    ddLOC.DataBind()

    If Not emp.Level = Employee.PermissionLevel.Admin Then
    Dim items As ListItemCollection = New ListItemCollection ' Copy to temp list

    For Each item As ListItem In ddLOC.Items
    If emp.Locations.Contains(item.Value) Then
    items.Add(item)
    End If
    Next

    ddLOC.Items.Clear() ' Empty DDL

    For Each newItem As ListItem In items
    ddLOC.Items.Add(newItem)
    Next
    End If
    End If
    End Sub

    Thanks in advnce
    KROSS, Jan 21, 2008
    #7
    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. =?Utf-8?B?TWVyZGFhZA==?=

    Drop down list does not see the selected item!

    =?Utf-8?B?TWVyZGFhZA==?=, Oct 20, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    11,860
    =?Utf-8?B?TWVyZGFhZA==?=
    Oct 20, 2004
  2. Stephen Adam
    Replies:
    3
    Views:
    12,104
    Stephen Adam
    Nov 30, 2004
  3. weiwei
    Replies:
    0
    Views:
    1,009
    weiwei
    Jan 5, 2007
  4. Geary
    Replies:
    3
    Views:
    4,878
    Brandon Gano
    Jul 21, 2007
  5. Replies:
    0
    Views:
    174
Loading...

Share This Page