Results 1 to 7 of 7

Thread: dropdownlist question *[RESOLVED]*

  1. #1

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306

    Question dropdownlist question *[RESOLVED]*

    i have a question for you .netters out there. i am using a dropdownlist on a webform and loading the values from a database. works fine.

    what i want to do is jump to an item in the list by using the "findbyvalue" function. e.g:

    Code:
    ddlCountys.Items.FindByValue(rs("CountyID").ToString()).Selected = True
    this failed. i was told something along the lines of "an item is already selected so u cant do that"

    i resolved this by using "ddl.selecteditem.selected = false", the result being:

    Code:
    ddlCountys.SelectedItem.Selected = False
    ddlCountys.Items.FindByValue(rs("CountyID").ToString()).Selected = True
    there is something about having to do that which irritates me, i dont want two lines of code

    whats a better way please?
    Last edited by tailz; Jun 18th, 2003 at 02:47 AM.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    lol i recon for 2 lines of code you wont find much better to try , but you could make it all 1 line
    Code:
    ddlCountys.SelectedItem.Selected = False : ddlCountys.Items.FindByValue(rs("CountyID").ToString()).Selected = True
    just put a space then a : at the end of the first line then another space, then add your other line and it'll all be on 1 line ( but still the same amount of code
    )
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306
    yea could do but its not really about the space it takes up on the screen.

    i'm more annoyed that I dont seem to be able to set the selected item without unselecting the current one.

    is there a way?

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Thats what you have to deal with. The SelectedItems property is a collection, so when you take the second item and tell it to be selected, the dropdown list tries to add the item to the collection. Windows complains becuase there is already one selected, and the maximum you can have selected is one.

    If you don't want to do that, you could always write your own control, so then you would only have to write one line of code each time.

  5. #5

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306

    Thumbs up result

    I read somewhere in knowledge base that on a dropdownlist, you (obviously) cant have more than 1 item selected nor can you not have no items selected.

    As setting .selected=true on an unselected item doesnt set .selected=false on the selecteditem, I couldn't use that as multiple items would be selected.

    I couldn't do .selected=false on the .selecteditem either, as that would mean no items were selected, which again was against the rules.

    Eventually just played with the .selectedindex property and came up with this:

    Code:
    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value))
    This works fine until .FindByValue returns nothing (if it doesnt find the item) so I put it all into a function. the result being:

    Code:
      Public Sub SetforFindbyValue(ByVal ddl As DropDownList, ByVal value As String)
    
        Dim xItem As ListItem = ddl.Items.FindByValue(value)
        If Not ddl Is Nothing Then
          ddl.SelectedIndex = ddl.Items.IndexOf(xitem)
        Else
          ddl.SelectedIndex = 0
        End If
    
      End Sub
    solves my problem anyway

    thanks for any replies

    tailz

  6. #6
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    Re: dropdownlist question *[RESOLVED]*

    Originally posted by tailz
    i have a question for you .netters out there. i am using a dropdownlist on a webform and loading the values from a database. works fine.

    what i want to do is jump to an item in the list by using the "findbyvalue" function. e.g:

    Code:
    ddlCountys.Items.FindByValue(rs("CountyID").ToString()).Selected = True
    this failed. i was told something along the lines of "an item is already selected so u cant do that"

    i resolved this by using "ddl.selecteditem.selected = false", the result being:

    Code:
    ddlCountys.SelectedItem.Selected = False
    ddlCountys.Items.FindByValue(rs("CountyID").ToString()).Selected = True
    there is something about having to do that which irritates me, i dont want two lines of code

    whats a better way please?

    I have similar question here. Would you mind taking a look at it?

    Thanks

    http://www.vbforums.com/showthread.p...hreadid=251306

  7. #7
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    This works from Microsoft.

    Code:
        Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
            If e.Item.ItemType = ListItemType.EditItem Then
                Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
                Dim currentgenre As String = CType(drv("TypeName"), String)' Name of the item you are looking for
                Dim ddl As DropDownList
                ddl = CType(e.Item.FindControl("dwnType"), DropDownList)' Name of the control you are looking for
                ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(currentgenre))' Here is where it preselects the control
            End If
        End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width