By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

Maintaining DropDownList selected index

P: n/a
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!

Apr 10 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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.

"Phuff" wrote:
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!

Apr 10 '06 #2

P: n/a
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.

Apr 10 '06 #3

P: n/a
Why not just bind it instead, then it will maintain ViewState?

"Phuff" wrote:
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.

Apr 10 '06 #4

P: n/a
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

Apr 10 '06 #5

P: n/a
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.

Apr 10 '06 #6

P: n/a
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.


"Phuff" wrote:
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

Apr 10 '06 #7

P: n/a
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?

Apr 10 '06 #8

P: n/a
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.

Apr 10 '06 #9

P: n/a
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.

Apr 10 '06 #10

P: n/a
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."

Phuff wrote:
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.


Apr 10 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.