DropDownList: SelectedItem is always top item

S

Sam C

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
 
S

Sam C

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
 
G

gaidar

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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top