DropDownList not retaining its selectedItem.Value property?

Discussion in 'ASP .Net' started by Andy B., Apr 1, 2009.

  1. Andy B.

    Andy B. Guest

    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"


    End Sub

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

    Dim TZ As TimeZoneInfo =


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

    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)")


    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., Apr 1, 2009
    1. Advertisements

  2. Andy B.

    Lloyd Sheen Guest

    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

    Lloyd Sheen, Apr 1, 2009
    1. Advertisements

  3. Andy B.

    Andy B. Guest

    DUH! I totally forgot about that....
    Andy B., Apr 1, 2009
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.