Maintaining DropDownList selected index

P

Phuff

Thanks in advance! I'm trying to maintain a dl list's selected index
on postback. What I'm doing is when a person selects an item from the
drop down list I select a date in a calendar control and populate a
text box with the date. To do this I set auto postback to the drop
down list. But it always resets the selected index on post back. I
control for this in the onLoad method...

If Not Page.IsPostBack Then

If page is posted back I select the date and populate textbox.text
correctly. Selected index is lost. I even added a key to the
viewstate with the selected index and tried grabbing the value from
that, but still no go. Help please!
 
G

Guest

Drop down lists do usually maintain their viewstate unless you re-bind them
to their datasource. Howeve i have found that if your DDL is bound to a
DataSourceControl, and your DDL is within another control (such as in a
template field of a DetailsView), then if you do something such as updatting
the headerText of one of the Fields in the detailsview control then it
re-sets the view state of all child controls within parent control.
 
P

Phuff

Thanks, but its not bound. What I do is loop through dataset grabbed
from a sql query, and then do an Items.add method.

I have actually found that it will maintain 2 selected states. One
labeled 7 and 1. The first index is a text field describing all items.
Then I add the other fields which are numbers. The value is also set
to equal the number listed as a string in the field. OIn my test
example I have a string..."Box/Unit ID" and then an item for 1-8. If I
select 7 it will actually keep this index. Everything else defaults to
1.
 
P

Phuff

Ok, I'll try it.. getting nothing on the bind... is this the correct
way to do so? (As you can see I commented out part of the old way)

Dim sqlSel As String = "SELECT Box_Id FROM Job_Order_Box WHERE
Order_ID=" + txtHidden.Text

'create new datasets and data adapters
Dim dsBox As New DataSet
Dim daBox As New OdbcDataAdapter(sqlSel, strCon)

'fill first dataset and populate drop down list with engineers
daBox.Fill(dsBox)

'dlBox.Items.Add("All Units/Boxes")
'dlBox.Items(0).Value = "0"
dlBox.DataSource = dsBox.Tables(0).DefaultView
 
P

Phuff

Ok got it figured out...it is databound.


I still have the issue.

I select Box_Id column and fill the ddl. On the particular order I'm
processing I have 1-8. Everything under 7 defaults to 1, and 7 and 8
default to 7. Its bizzare. It will only get those two indices.
 
G

Guest

You need to set the DataTextField and DataValueField to the relevent fields.
You also have to call the DataBind method, but make sure you put it in a
(sorry i don't speak VB) if (!IsPostBack) section, so that it does not
re-bind everytime the page is posted back.
 
P

Phuff

Ya I did that, and I get the control as databound no problem. But it
is still defaulting to those values. Actually its the 0 index and 6
index. Sure databound works just as well as my other method did, but
the selected index is not being maintained correctly. Is this a .net
2.0 issue?
 
P

Phuff

I also did some javascript to get the index client side and see what it
was. the ddl will only return 2 indices out of the 7 available. If I
set the selected index initially it will be selected correctly. I
select a new item and it then defaults to one of the two indices. So
bizzare. Never had an issue like this before.
 
P

Phuff

Also, I have another DDL on the page that I fill in the same manner and
it doesn't have these issues. I will keep digging.
 
P

Phuff

Problem solved! Apparently if you have any duplicates in your Value
collection the selected index that is maintained through the state
defaults to the first index in that group of duplicates.

So my items correspond to the Box_Id in the table (the unique
identifier in the DB). There are 8 boxes, 1-8. Then I grab a business
related date and fill the Value collection with this, that date
corresponds to something with each box. The dates (which become
values) could all be different or the same, there is no consistency
with the dates. Well boxes 1-6 all had the same recieve date so the
value was the same for all those items. Therefore, if you select
anything within "Index" 0-5 it will default to 0 because that is the
first item in the list with the duplicated date. Same for "Index" 6-7.
So when it is maintaining its state it remembers the selected index,
but won't allow duplicates in the value field. They should provide a
property which allows duplicates in the value field."
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top