DropDownList not retaining its selectedItem.Value property?

  • Thread starter Thread starter Andy B.
  • Start date Start date
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?
 
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
 
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
 
Back
Top