Dear homebuilder-ga,
Yes, what you want is indeed the SelectedIndex - essentially, you need
to capture it when a selection is made from the drop-down list, and
store it for use when the browser comes back to the page; and when the
page loads, check to see if you have a stored selection, and set the
list box to that. The easiest place to store the current selection is
in the session state for your web application.
Here's how it's done:
First, set the "AutoPostBack" property of your drop-down list to
"true". This ensures that the user's selection will be sent back to
the web server so that you'll be able to capture it.
Then, add the following code to the SelectedIndexChanged event of your
drop-down list:
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles
DropDownList1.SelectedIndexChanged
Session.Add("listValue", DropDownList1.SelectedIndex)
End Sub
(Change DropDownList1 to the name of your specific drop-down list, and
listValue to something unique in your application, of course.)
And add the following code to the Page_Load event of your web form,
after
the code which populates the drop-down list:
Dim Index As Int32
' Only do this if the page is being loaded from another page, not
' from itself. Important, otherwise we wouldn't be able to change
' the value at all.
If Not Me.IsPostBack Then
Index = Session.Item("listValue")
' To avoid throwing an exception, check that the number of items
in
' the list hasn't shrunk since we were last on this page. Don't
set
' the index if it would now fall off the end.
If (Index < DropDownList1.Items.Count) Then
DropDownList1.SelectedIndex = Index
End If
End If
(Making the same changes as above.)
And that should now work!
The drawback with this method is that it only stores the index of the
selected item - thus, if you delete an item from the drop-down - in
your case, from your database - or the order they're returned in
changes, the indices of every item in the drop-down list changes, and
you'll end up with the wrong one being shown when you come back to the
page.
There's a way to get around this by using the SelectedValue property,
which stores the value by name. To use this method, follow the above
method, but substitute the following code:
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles
DropDownList1.SelectedIndexChanged
Session.Add("listValue", DropDownList1.SelectedValue)
End Sub
and:
Dim Value As String
' Only do this if the page is being loaded from another page, not
' from itself. Important, otherwise we wouldn't be able to change
' the value at all.
If Not Me.IsPostBack Then
Value = Session.Item("listValue")
' ArgumentOutOfRangeException will be thrown if the value no
longer
' exists.
Try
DropDownList1.SelectedValue = Value
Catch ex As ArgumentOutOfRangeException
' Ignore this exception - it just means value no longer
exists.
' Go with default value.
End Try
End If
And that should again work for you.
If this answer isn't quite what you're looking for, please feel free
to request a clarification,
cerebrate-ga |
Clarification of Answer by
cerebrate-ga
on
27 Feb 2003 08:11 PST
Dear homebuilder-ga,
I've just double-checked, and I think I see the problem; the
SelectedValue property was added in release 1.1 of the .NET framework
- if you're using 1.0, it won't be present. Sorry about that.
However, there's a slightly longer way of expressing the same thing
which should hopefully work for you. Instead of saving the value, save
the list item itself in the SelectedIndexChanged event:
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles
DropDownList1.SelectedIndexChanged
Session.Add("listItem", DropDownList1.SelectedItem)
End Sub
and in the Page_Load event, we look up the index of that particular
item, and then set SelectedIndex to that index, thus:
If Not Me.IsPostBack Then
Item = Session.Item("listItem")
Dim index As Int32
' Find the index of the list item we saved
index = DropDownList1.Items.IndexOf(Item)
If (index <> -1) Then
' If the item is still in the list, set it selected
DropDownList1.SelectedIndex = index
End If
End If
I've used the session state here, again, but you should be able to
swap your database code in here without any trouble.
And hopefully that should work!
cerebrate-ga
|
Clarification of Answer by
cerebrate-ga
on
27 Feb 2003 21:36 PST
Dear homebuilder-ga,
Yes, that should also be possible. In fact, you don't even need to
loop, as the ListItemsCollection supplies a FindByValue method to let
you do exactly this.
The code you would need in Page.Load is (where previousValue is a
variable containing the value from your database):
If Not Me.IsPostBack Then
Dim item As ListItem
' Find the ListItem with the value from the database
item = DropDownList1.Items.FindByValue (previousValue)
If Not (item Is Nothing) Then
' If it exists, set SelectedIndex to its index
DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(item)
End If
End If
(To clarify the previous one, SelectedItem is the currently selected
ListItem, which is the object used internally by the DropDownList to
manage its items (it contains a few other properties apart from the
value itself) - when you call Add with a string, it creates one for
you with sensible defaults. It's probably the most useful thing to
store in 1.0 to avoid problems with the list changing, except when, as
here, you have the information stored elsewhere anyway...)
Regards,
cerebrate-ga
|