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

M

Marina

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

paul crowder

Marina said:
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
 
M

Marina

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.
 
P

paul crowder

Marina said:
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
 
M

Marina

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.
 
N

Nick

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"

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.
 
Joined
Jan 21, 2008
Messages
1
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top