|
-
Jun 15th, 2003, 11:07 AM
#1
Thread Starter
Hyperactive Member
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.
-
Jun 15th, 2003, 03:06 PM
#2
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]
-
Jun 16th, 2003, 09:49 AM
#3
Thread Starter
Hyperactive Member
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?
-
Jun 17th, 2003, 01:18 PM
#4
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.
-
Jun 18th, 2003, 02:41 AM
#5
Thread Starter
Hyperactive Member
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
-
Jun 27th, 2003, 02:01 PM
#6
PowerPoster
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
-
Jun 30th, 2003, 08:29 AM
#7
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|