473,769 Members | 5,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Maintaining DropDownList selected index

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
10 2746
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
DataSourceContr ol, 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
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
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
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(dsBo x)

'dlBox.Items.Ad d("All Units/Boxes")
'dlBox.Items(0) .Value = "0"
dlBox.DataSourc e = dsBox.Tables(0) .DefaultView

Apr 10 '06 #5
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
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(dsBo x)

'dlBox.Items.Ad d("All Units/Boxes")
'dlBox.Items(0) .Value = "0"
dlBox.DataSourc e = dsBox.Tables(0) .DefaultView

Apr 10 '06 #7
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
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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2805
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option Description"></asp:BoundColumn> <asp:TemplateColumn runat="server" HeaderText="Id Type Option" "> <itemtemplate> <asp:label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TypeOption") %>' /> <asp:label runat="server" ID="LlbTypeOption" Visible=False...
6
7021
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone changes box1, I want to set the selected index in box2 = 0. When I do this, I dont get an error, but when the page loads, it still has the selected value and not 0. It seems that it is getting the selected value from the viewstate
4
529
by: sklett | last post by:
I have 2 dropdownlist on a page. They have ViewState = true. After I submit to the page, the selection in the DropDownList is lost. I set a break in the Page_Load event to make sure that I wasn't re-binding them somehow... I'm not. So shouldn't they maintain their selection? Anything you can think of to check? Thanks- Steve
10
10771
by: dhnriverside | last post by:
Hi guys Still having a problem with this dropdownlist. Basically, I've got 4. The first 2 work fine, then my code crashes on the 3rd. ddlEndTimeHour.Items.FindByValue(endTime).Selected = true; Where endTime is a string containing "15".
4
2107
by: wolfgang wagner | last post by:
hi all! after successfully integrating a dropdownlist in my datagrid i have another problem: i cannot set the selected index of the dropdownlistbox. here is my code: hardware.aspx -------------
1
4440
by: Paul L | last post by:
Hi, I have an issue with the OnSelectedIndexChanged event not firing for a DropDownList control which is in the ItemTemplate of a DataList. I have made an exact copy of the DropDownList control, and placed it outside of the DataList and it fires the event just fine. So it's definitely to do with it being in a template. I've managed to reproduce the issue on a small test app, the code and html is below:
11
5815
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
1
4659
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button ,another row will be created with the same control (I mean another dropdown and 2 button) and so on. and by pressing Remove button the selecetd row will be removed. I used viewstate to keep my value for postback, I want by changing selectedvalue of...
0
3501
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button ,another row will be created with the same control (I mean another dropdown and 2 button) and so on. and by...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.