You must set the selected index - meaning, you must know the location of
the item that needs to be selected in the list.
"ctb" <clark@dotnet.itags.org.speakeasyinteractive.com> wrote in message
news:020201c34d4c$ab83f130$a101280a@dotnet.itags.org.phx.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" <zlatkinam@.nospam.hotmail.com> wrote in message
news:ugcW47UTDHA.1552@.TK2MSFTNGP10.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
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.crowder@.acstechnologies.com> wrote in message
news:Od%23WoEVTDHA.2480@.tk2msftngp13.phx.gbl...
> "Marina" <zlatkinam@.nospam.hotmail.com> wrote in message
> news:ugcW47UTDHA.1552@.TK2MSFTNGP10.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" <zlatkinam@.nospam.hotmail.com> wrote in message
news:%23GADNKVTDHA.2148@.TK2MSFTNGP10.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.ClearSelection();
ddl.Items.FindByValue("somevalue").Selected = true;
You should be okay.
Paul
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.crowder@.acstechnologies.com> wrote in message
news:OymHswVTDHA.2148@.TK2MSFTNGP12.phx.gbl...
> "Marina" <zlatkinam@.nospam.hotmail.com> wrote in message
> news:%23GADNKVTDHA.2148@.TK2MSFTNGP10.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.ClearSelection();
> ddl.Items.FindByValue("somevalue").Selected = true;
> You should be okay.
> Paul
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"
<zlatkinam@.nospam.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.crowder@.acstechnologies.com> wrote in message
news:OymHswVTDHA.2148@.TK2MSFTNGP12.phx.gbl...
> "Marina" <zlatkinam@.nospam.hotmail.com> wrote in message
> news:%23GADNKVTDHA.2148@.TK2MSFTNGP10.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.ClearSelection();
> ddl.Items.FindByValue("somevalue").Selected = true;
> You should be okay.
> Paul
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment