Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Maintaining DropDownList selected index

Reply
Thread Tools

Maintaining DropDownList selected index

 
 
Phuff
Guest
Posts: n/a
 
      04-10-2006
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!

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Y2xpY2tvbg==?=
Guest
Posts: n/a
 
      04-10-2006
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!
>
>

 
Reply With Quote
 
 
 
 
Phuff
Guest
Posts: n/a
 
      04-10-2006
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.

 
Reply With Quote
 
=?Utf-8?B?Y2xpY2tvbg==?=
Guest
Posts: n/a
 
      04-10-2006
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.
>
>

 
Reply With Quote
 
Phuff
Guest
Posts: n/a
 
      04-10-2006
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

 
Reply With Quote
 
Phuff
Guest
Posts: n/a
 
      04-10-2006
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.

 
Reply With Quote
 
=?Utf-8?B?Y2xpY2tvbg==?=
Guest
Posts: n/a
 
      04-10-2006
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
>
>

 
Reply With Quote
 
Phuff
Guest
Posts: n/a
 
      04-10-2006
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?

 
Reply With Quote
 
Phuff
Guest
Posts: n/a
 
      04-10-2006
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.

 
Reply With Quote
 
Phuff
Guest
Posts: n/a
 
      04-10-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help to fill textboxes from a selected item in datagrid-selected index changed. mldardy ASP .Net 0 09-28-2010 02:59 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C++ 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C Programming 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui Python 0 04-27-2009 12:46 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM



Advertisments