473,657 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2062

Could you post Page_Load code here?

Gaidar

"Sam C" <th***********@ yahoo.co.uk> wrote in message
news:42******** *************** @ptn-nntp-reader01.plus.n et...
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.EventArg s) Handles MyBase.Load
If Session("user") Is Nothing Then
Response.Redire ct("Default.asp x")
End If
If Not Page.IsPostBack Then
PopulateForm()
End If
End Sub

Private Sub dgrdParticipant s_ItemDataBound (ByVal sender As System.Object,
ByVal e As DataGridItemEve ntArgs) Handles dgrdParticipant s.ItemDataBound
'add the client side confirmation for the delete link
If e.Item.ItemType <> ListItemType.He ader AndAlso e.Item.ItemType <>
ListItemType.Fo oter AndAlso e.Item.ItemType <> ListItemType.Ed itItem Then
Dim deleteButton As LinkButton =
e.Item.Cells(6) .FindControl("l nkDeletePartici pant")
Dim oParticipant As cParticipant = e.Item.DataItem
deleteButton.At tributes("oncli ck") = "javascript:ret urn confirm('Delete " &
oparticipant.Fo rename & " " & oparticipant.Su rname & "?')"
End If
'load the combo boxes
'if the row is the one being edited
If e.Item.ItemType = ListItemType.Ed itItem Then
Dim oParticipant As cParticipant = e.Item.DataItem
Dim cboGender As DropDownList = e.Item.Cells(3) .Controls(1)
cboGender = LoadGenders(cbo Gender, oParticipant.Ge nderID)
Dim cboTicketType As DropDownList = e.Item.Cells(5) .Controls(1)
cboTicketType = LoadTicketTypes (cboTicketType, oParticipant.Ti cketTypeID)
End If
'if the row is the footer
If e.Item.ItemType = ListItemType.Fo oter 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(cbo Gender)
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.Cr eateNew(Session ("group").Ca mp)
liItem = New ListItem
liItem.Text = oTicketType.ToS tring
liItem.Value = oTicketType.ID. ToString

cboTicketType.I tems.Add(liItem )
Next
cboTicketType.I D = "cboTicketT ype"
SetDropDownSele cted(cboTicketT ype, currentValue)
Return cboTicketType
End Function
Private Sub SetDropDownSele cted(ByVal ddl As DropDownList, ByVal value As
String)
Dim li As ListItem
li = ddl.Items.FindB yValue(value)
If Not li Is Nothing Then
ddl.SelectedInd ex = ddl.Items.Index Of(li)
End If
End Sub

"gaidar" <ga****@vbstree ts.ru> wrote in message
news:ew******** ******@TK2MSFTN GP09.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.n et...
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.EventArg s)
Handles Me.Load

If Not Page.IsPostBack Then

PopulateForm()

End If

End Sub

Private Sub PopulateForm()

DropDownList1.I tems.Add("Strin g 1")

DropDownList1.I tems.Add("Strin g 2")

DropDownList1.I tems.Add("Strin g 3")

End Sub

Protected Sub Button1_Click(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button1.Click

Response.Write( DropDownList1.S electedValue)

End Sub
"Sam C" <th***********@ yahoo.co.uk> wrote in message
news:42******** *************** @ptn-nntp-reader01.plus.n et...
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.EventArg s) Handles MyBase.Load
If Session("user") Is Nothing Then
Response.Redire ct("Default.asp x")
End If
If Not Page.IsPostBack Then
PopulateForm()
End If
End Sub

Private Sub dgrdParticipant s_ItemDataBound (ByVal sender As System.Object,
ByVal e As DataGridItemEve ntArgs) Handles dgrdParticipant s.ItemDataBound
'add the client side confirmation for the delete link
If e.Item.ItemType <> ListItemType.He ader AndAlso e.Item.ItemType <>
ListItemType.Fo oter AndAlso e.Item.ItemType <> ListItemType.Ed itItem Then
Dim deleteButton As LinkButton =
e.Item.Cells(6) .FindControl("l nkDeletePartici pant")
Dim oParticipant As cParticipant = e.Item.DataItem
deleteButton.At tributes("oncli ck") = "javascript:ret urn confirm('Delete "
& oparticipant.Fo rename & " " & oparticipant.Su rname & "?')"
End If
'load the combo boxes
'if the row is the one being edited
If e.Item.ItemType = ListItemType.Ed itItem Then
Dim oParticipant As cParticipant = e.Item.DataItem
Dim cboGender As DropDownList = e.Item.Cells(3) .Controls(1)
cboGender = LoadGenders(cbo Gender, oParticipant.Ge nderID)
Dim cboTicketType As DropDownList = e.Item.Cells(5) .Controls(1)
cboTicketType = LoadTicketTypes (cboTicketType, oParticipant.Ti cketTypeID)
End If
'if the row is the footer
If e.Item.ItemType = ListItemType.Fo oter 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(cbo Gender)
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.Cr eateNew(Session ("group").Ca mp)
liItem = New ListItem
liItem.Text = oTicketType.ToS tring
liItem.Value = oTicketType.ID. ToString

cboTicketType.I tems.Add(liItem )
Next
cboTicketType.I D = "cboTicketT ype"
SetDropDownSele cted(cboTicketT ype, currentValue)
Return cboTicketType
End Function
Private Sub SetDropDownSele cted(ByVal ddl As DropDownList, ByVal value As
String)
Dim li As ListItem
li = ddl.Items.FindB yValue(value)
If Not li Is Nothing Then
ddl.SelectedInd ex = ddl.Items.Index Of(li)
End If
End Sub

"gaidar" <ga****@vbstree ts.ru> wrote in message
news:ew******** ******@TK2MSFTN GP09.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.n et...
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
2779
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...
0
2422
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 arbitrary number of empty rows to be added to it. So, if a user knew in advance that they wanted to add 3 phone numbers, they can type the nuber '3' in a textbox (outside the repeater) and click a button (also outside of a repeater), and 3 new empty ...
6
3917
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 the value. I am trying to insert the Text from this control into a field in a database, and it perpetually inserts the text from the first item of the list instead of the selected item. What the heck could I be missing here?
4
4672
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 this: ddlMyDdl.SelectedItem The problem is that I am ALWAYS getting the first item back! Even when I select the 5th item, the 10th item, etc....
7
2032
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 time to allow me to have the first option in the list a ‘listitem’ saying something like ‘please pick an option’, and then the rest of options coming from the database. <asp:dropdownlist id="fm_Category" runat="server"...
10
10736
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".
3
2695
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 set to a predetermined value. I can set the SelectedItem property easy enough and get the dropdownlist to work fine just so long as the dropdownlist is just on the form itself not in a DataList control.
3
1053
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 the arg e like this: CType(e.Item.Cells.Item(3).Controls(1), DropDownList).SelectedItem.Text.Trim. The SelectedItem is always equal the first item in the dropdownlist, not the one i highlighted. Does anyone know why the dropdownlist is not...
2
2181
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 OnItemDataBound I can fill it but it is impossible for me to load the right selectedItem.Value , infact looking at the html page produced by the server I've this strange code : <select name="MyDataCampi:_ctl1:ComboTipoPartita"...
0
8319
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,...
0
8837
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7347
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
6175
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
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
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
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.