DropDownList not retaining its selectedItem.Value property?

A

Andy B.

I populated a DropDownList with Timezones from the server the page is hosted
on. I then have a linkButton to populate a bulleted list with some of the
timezone properties. The problem is that the first item in the list is
always the selected one (or at least it looks that way).

1. On an aspx page, drop the following controls
- DropDownList (TimeZonesList)
- LinkButton (OKButton)
- BulletedList (TimezoneDetails)
2. put the following code in the codebehind and run the page
Imports System

Imports System.Collections.Generic

Partial Public Class _Default

Inherits System.Web.UI.Page

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

TimeZonesList.DataSource = TimeZoneInfo.GetSystemTimeZones()

TimeZonesList.DataTextField = "DisplayName"

TimeZonesList.DataValueField = "ID"

TimeZonesList.DataBind()

End Sub

Protected Sub OkButton_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles OkButton.Click

Dim TZ As TimeZoneInfo =
TimeZoneInfo.FindSystemTimeZoneById(TimeZonesList.SelectedItem.Value)

TimeZoneDetailsList.Items.Clear()

TimeZoneDetailsList.Items.Add(TZ.BaseUtcOffset.ToString() +
"(BaseUTCOffset)")

TimeZoneDetailsList.Items.Add(TZ.DaylightName + "(Daylight name)")

TimeZoneDetailsList.Items.Add(TZ.DisplayName + "(DisplayName)")

TimeZoneDetailsList.Items.Add(TZ.Id + "(ID)")

TimeZoneDetailsList.Items.Add(TZ.StandardName + "(StandardName)")

TimeZoneDetailsList.DataBind()

End Sub

End Class

3. Run the page, select a timezone and click ok. The first one in the list
is always shown in the bulleted list. How do you fix this?
 
L

Lloyd Sheen

Andy B. said:
I populated a DropDownList with Timezones from the server the page is
hosted on. I then have a linkButton to populate a bulleted list with some
of the timezone properties. The problem is that the first item in the list
is always the selected one (or at least it looks that way).

1. On an aspx page, drop the following controls
- DropDownList (TimeZonesList)
- LinkButton (OKButton)
- BulletedList (TimezoneDetails)
2. put the following code in the codebehind and run the page
Imports System

Imports System.Collections.Generic

Partial Public Class _Default

Inherits System.Web.UI.Page

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

TimeZonesList.DataSource = TimeZoneInfo.GetSystemTimeZones()

TimeZonesList.DataTextField = "DisplayName"

TimeZonesList.DataValueField = "ID"

TimeZonesList.DataBind()

End Sub

Protected Sub OkButton_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles OkButton.Click

Dim TZ As TimeZoneInfo =
TimeZoneInfo.FindSystemTimeZoneById(TimeZonesList.SelectedItem.Value)

TimeZoneDetailsList.Items.Clear()

TimeZoneDetailsList.Items.Add(TZ.BaseUtcOffset.ToString() +
"(BaseUTCOffset)")

TimeZoneDetailsList.Items.Add(TZ.DaylightName + "(Daylight name)")

TimeZoneDetailsList.Items.Add(TZ.DisplayName + "(DisplayName)")

TimeZoneDetailsList.Items.Add(TZ.Id + "(ID)")

TimeZoneDetailsList.Items.Add(TZ.StandardName + "(StandardName)")

TimeZoneDetailsList.DataBind()

End Sub

End Class

3. Run the page, select a timezone and click ok. The first one in the list
is always shown in the bulleted list. How do you fix this?

You need to put the load code into a:

If not IsPostBack then

Your code which only run once at the first load of the page

End If

Your currrent code is resetting the state of the list each time there is a
postback.

LS
 
A

Andy B.

DUH! I totally forgot about that....
Lloyd Sheen said:
You need to put the load code into a:

If not IsPostBack then

Your code which only run once at the first load of the page

End If

Your currrent code is resetting the state of the list each time there is a
postback.

LS
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top