473,412 Members | 5,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

DropDownList: SelectedItem is always top item

Hi,

I have an ASP.Net page which has a DropDownList on it. The DDL is populated
via a method which is called from the Page_Load if IsPostBack = False.

When the form is submitted the SelectedItem of the DDL (as retrieved from
any of the supplied methods) is always the top item regardless of which one
is actually selected. The odd thing is that any resubmissions of the form
produce the correct SelectedItem.

Any ideas?

Sam
Nov 19 '05 #1
3 2044

Could you post Page_Load code here?

Gaidar

"Sam C" <th***********@yahoo.co.uk> wrote in message
news:42***********************@ptn-nntp-reader01.plus.net...
Hi,

I have an ASP.Net page which has a DropDownList on it. The DDL is
populated via a method which is called from the Page_Load if IsPostBack =
False.

When the form is submitted the SelectedItem of the DDL (as retrieved from
any of the supplied methods) is always the top item regardless of which
one is actually selected. The odd thing is that any resubmissions of the
form produce the correct SelectedItem.

Any ideas?

Sam

Nov 19 '05 #2
Here you go, I've included the other bits which could be the culprit as
well.

Many thanks for your help,
Sam

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Session("user") Is Nothing Then
Response.Redirect("Default.aspx")
End If
If Not Page.IsPostBack Then
PopulateForm()
End If
End Sub

Private Sub dgrdParticipants_ItemDataBound(ByVal sender As System.Object,
ByVal e As DataGridItemEventArgs) Handles dgrdParticipants.ItemDataBound
'add the client side confirmation for the delete link
If e.Item.ItemType <> ListItemType.Header AndAlso e.Item.ItemType <>
ListItemType.Footer AndAlso e.Item.ItemType <> ListItemType.EditItem Then
Dim deleteButton As LinkButton =
e.Item.Cells(6).FindControl("lnkDeleteParticipant" )
Dim oParticipant As cParticipant = e.Item.DataItem
deleteButton.Attributes("onclick") = "javascript:return confirm('Delete " &
oparticipant.Forename & " " & oparticipant.Surname & "?')"
End If
'load the combo boxes
'if the row is the one being edited
If e.Item.ItemType = ListItemType.EditItem Then
Dim oParticipant As cParticipant = e.Item.DataItem
Dim cboGender As DropDownList = e.Item.Cells(3).Controls(1)
cboGender = LoadGenders(cboGender, oParticipant.GenderID)
Dim cboTicketType As DropDownList = e.Item.Cells(5).Controls(1)
cboTicketType = LoadTicketTypes(cboTicketType, oParticipant.TicketTypeID)
End If
'if the row is the footer
If e.Item.ItemType = ListItemType.Footer Then
Dim cboTicketType As DropDownList = e.Item.Cells(5).Controls(1)
cboTicketType = LoadTicketTypes(cboTicketType)
Dim cboGender As DropDownList = e.Item.Cells(3).Controls(1)
cboGender = LoadGenders(cboGender)
End If
End Sub

Private Function LoadTicketTypes(ByVal cboTicketType As DropDownList,
Optional ByVal currentValue As Int32 = -1) As DropDownList
Dim oTicketType As cTicketType
Dim liItem As ListItem
For Each oTicketType In cTicketTypes.CreateNew(Session("group").Camp)
liItem = New ListItem
liItem.Text = oTicketType.ToString
liItem.Value = oTicketType.ID.ToString

cboTicketType.Items.Add(liItem)
Next
cboTicketType.ID = "cboTicketType"
SetDropDownSelected(cboTicketType, currentValue)
Return cboTicketType
End Function
Private Sub SetDropDownSelected(ByVal ddl As DropDownList, ByVal value As
String)
Dim li As ListItem
li = ddl.Items.FindByValue(value)
If Not li Is Nothing Then
ddl.SelectedIndex = ddl.Items.IndexOf(li)
End If
End Sub

"gaidar" <ga****@vbstreets.ru> wrote in message
news:ew**************@TK2MSFTNGP09.phx.gbl...

Could you post Page_Load code here?

Gaidar

"Sam C" <th***********@yahoo.co.uk> wrote in message
news:42***********************@ptn-nntp-reader01.plus.net...
Hi,

I have an ASP.Net page which has a DropDownList on it. The DDL is
populated via a method which is called from the Page_Load if IsPostBack =
False.

When the form is submitted the SelectedItem of the DDL (as retrieved from
any of the supplied methods) is always the top item regardless of which
one is actually selected. The odd thing is that any resubmissions of the
form produce the correct SelectedItem.

Any ideas?

Sam


Nov 19 '05 #3

I tried an simplified example and everything works fine:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If Not Page.IsPostBack Then

PopulateForm()

End If

End Sub

Private Sub PopulateForm()

DropDownList1.Items.Add("String 1")

DropDownList1.Items.Add("String 2")

DropDownList1.Items.Add("String 3")

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Response.Write(DropDownList1.SelectedValue)

End Sub
"Sam C" <th***********@yahoo.co.uk> wrote in message
news:42***********************@ptn-nntp-reader01.plus.net...
Here you go, I've included the other bits which could be the culprit as
well.

Many thanks for your help,
Sam

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Session("user") Is Nothing Then
Response.Redirect("Default.aspx")
End If
If Not Page.IsPostBack Then
PopulateForm()
End If
End Sub

Private Sub dgrdParticipants_ItemDataBound(ByVal sender As System.Object,
ByVal e As DataGridItemEventArgs) Handles dgrdParticipants.ItemDataBound
'add the client side confirmation for the delete link
If e.Item.ItemType <> ListItemType.Header AndAlso e.Item.ItemType <>
ListItemType.Footer AndAlso e.Item.ItemType <> ListItemType.EditItem Then
Dim deleteButton As LinkButton =
e.Item.Cells(6).FindControl("lnkDeleteParticipant" )
Dim oParticipant As cParticipant = e.Item.DataItem
deleteButton.Attributes("onclick") = "javascript:return confirm('Delete "
& oparticipant.Forename & " " & oparticipant.Surname & "?')"
End If
'load the combo boxes
'if the row is the one being edited
If e.Item.ItemType = ListItemType.EditItem Then
Dim oParticipant As cParticipant = e.Item.DataItem
Dim cboGender As DropDownList = e.Item.Cells(3).Controls(1)
cboGender = LoadGenders(cboGender, oParticipant.GenderID)
Dim cboTicketType As DropDownList = e.Item.Cells(5).Controls(1)
cboTicketType = LoadTicketTypes(cboTicketType, oParticipant.TicketTypeID)
End If
'if the row is the footer
If e.Item.ItemType = ListItemType.Footer Then
Dim cboTicketType As DropDownList = e.Item.Cells(5).Controls(1)
cboTicketType = LoadTicketTypes(cboTicketType)
Dim cboGender As DropDownList = e.Item.Cells(3).Controls(1)
cboGender = LoadGenders(cboGender)
End If
End Sub

Private Function LoadTicketTypes(ByVal cboTicketType As DropDownList,
Optional ByVal currentValue As Int32 = -1) As DropDownList
Dim oTicketType As cTicketType
Dim liItem As ListItem
For Each oTicketType In cTicketTypes.CreateNew(Session("group").Camp)
liItem = New ListItem
liItem.Text = oTicketType.ToString
liItem.Value = oTicketType.ID.ToString

cboTicketType.Items.Add(liItem)
Next
cboTicketType.ID = "cboTicketType"
SetDropDownSelected(cboTicketType, currentValue)
Return cboTicketType
End Function
Private Sub SetDropDownSelected(ByVal ddl As DropDownList, ByVal value As
String)
Dim li As ListItem
li = ddl.Items.FindByValue(value)
If Not li Is Nothing Then
ddl.SelectedIndex = ddl.Items.IndexOf(li)
End If
End Sub

"gaidar" <ga****@vbstreets.ru> wrote in message
news:ew**************@TK2MSFTNGP09.phx.gbl...

Could you post Page_Load code here?

Gaidar

"Sam C" <th***********@yahoo.co.uk> wrote in message
news:42***********************@ptn-nntp-reader01.plus.net...
Hi,

I have an ASP.Net page which has a DropDownList on it. The DDL is
populated via a method which is called from the Page_Load if IsPostBack
= False.

When the form is submitted the SelectedItem of the DDL (as retrieved
from any of the supplied methods) is always the top item regardless of
which one is actually selected. The odd thing is that any resubmissions
of the form produce the correct SelectedItem.

Any ideas?

Sam



Nov 19 '05 #4

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

Similar topics

12
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...
0
by: kamaumalone | last post by:
I have a dropdownlist which lives inside of a repeater. The repeater accepts user input via textboxes and the aforementioned dropdownlist. The repeater accepts phone numbers and allows for an...
6
by: Martin Schmid | last post by:
When testing for the value of my DropDownList.SelectedItem.Text - it always returns the Text of the first element of the list. It is a databound control, and I have a breakpoint in my code to test...
4
by: VB Programmer | last post by:
I have a dropdownlist which I populated on page_load. This works fine. I simply do a ddlMyDdl.Items.Add(xxx) I also have a button which references the CURRENTLY selected item in the ddl like...
7
by: Lastie | last post by:
Hi all, I’ve got a ‘dropdownlist’ web control and I can add ‘listitem’ no problem. I can also bind data from an SQL database fine. My problem is that I want to do both at the same...
10
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 =...
3
by: sling blade | last post by:
Hi, I have a dropdownlist in my DataList control and I am trying to set the SelectedItem property of the dropdownlist so when the datalist displays the user will see the text in the dropdownlist...
3
by: CsaaGuy | last post by:
Hi, I have a datagrid and each column i have a dropdownlist. I am populalting these dropdownlists in the itembound event. This part is working fine. However, when i press the update link, and use...
2
by: Antonio D'Ottavio | last post by:
Good morning, I've a problem with a dropdownlist located inside any row of a datalist, I fill both datalist and dropdownlist at runtime, the problem is with the dropdownlist infact using the event...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.