Web User Control Drop Down List does not show selected item

Discussion in 'ASP .Net Building Controls' started by stephg, Oct 27, 2005.

  1. stephg

    stephg Guest

    Hi,

    I have a user control that only contains a drop down list. This ddl is
    filled with countries. In my ASPX page the control displays properly and I
    can also get the selected item. However, when I want to select an item from
    the list by default that is not the first entry in the list, it does not show
    properly. It always shows the first item in the list selected (which is the
    default for ddls).
    When I get the selected value from the list, it shows the proper value. But
    it doesn t select it in the UI. What am I missing.
    Thanks a lot
    steph
    Below the code.
    ======
    Imports System.Data.OleDb
    Imports System.Configuration
    Public Class ddlCountries
    Inherits System.Web.UI.UserControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub
    Protected WithEvents ddlCountry As System.Web.UI.WebControls.DropDownList

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    If Not IsPostBack Then
    bindDDL()
    End If
    End Sub
    Private Sub bindDDL()
    ddlCountry.DataSource = getCountries()
    ddlCountry.DataTextField = "CountryName"
    ddlCountry.DataValueField = "CountryCode"
    ddlCountry.DataBind()
    End Sub
    Private Function getCountries() As DataSet

    ' Set up parameter for stored procedure
    Dim connection1 As New
    OleDbConnection(ConfigurationSettings.AppSettings.Item("OleDbConnection1.ConnectionString"))
    Dim command1 As New OleDbCommand
    command1.CommandType = CommandType.StoredProcedure
    command1.Connection = connection1
    Dim OleDbDataAdapter1 As New
    System.Data.OleDb.OleDbDataAdapter(command1)
    Dim ds As New DataSet
    ' Set up stored procedure

    command1.CommandText = "GetCountries"
    OleDbDataAdapter1.Fill(ds, "Countries")
    If ds.Tables(0).Rows.Count > 0 Then
    Return ds
    Else
    Return Nothing
    End If
    End Function
    Public ReadOnly Property GetSelectedValue() As Integer
    Get
    Return ddlCountry.SelectedValue
    End Get
    End Property
    Public WriteOnly Property SetSelectedValue()
    Set(ByVal Value)
    bindDDL()
    Dim i As Integer
    ddlCountry.SelectedIndex = 0
    For i = 0 To ddlCountry.Items.Count - 1
    If ddlCountry.Items(i).Value = Value Then
    ddlCountry.Items(i).Selected = True
    Else
    ddlCountry.Items(i).Selected = False
    End If
    Next
    End Set
    End Property
    End Class

    ======
    And the snippet from the ASPX page
    ======
    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="CreateCompany.aspx.vb" Inherits="Manage2Job.CreateCompany"%>
    <%@Register TagPrefix="countries" TagName="DDLCountry"
    Src="ddlCountries.ascx" %>

    .......
    <countries:ddlcountry id="ddlC1" runat="server"></countries:ddlcountry>
    ======
    And the code behind
    ======
    .....
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    If Not IsPostBack Then
    showData()
    end if
    end sub
    .....
    Sub showData()
    ....
    ' Country
    ddlC1.SetSelectedValue = dr("Country")
    ....
    end sub
    ......
    ======
    stephg, Oct 27, 2005
    #1
    1. Advertising

  2. stephg

    Paul Way Guest

    You'll have to excuse my lack of having an IDE on my computer so you won't be
    able to copy and paste this code...


    1)

    Public WriteOnly Property SetSelectedValue()
    Set(ByVal Value)
    bindDDL()
    Dim i As Integer
    ddlCountry.SelectedIndex = 0
    For i = 0 To ddlCountry.Items.Count - 1
    If ddlCountry.Items(i).Value = Value Then
    ddlCountry.Items(i).Selected = True 'I do not think you need
    this line
    ddlCountry.SelectedIndex = i
    exit for 'go ahead and exit the for loop for optimization
    End If
    Next
    End Set
    End Property


    Also, if that doesn't work then try the EnsureVisible property (I know that
    is on listviews)
    Paul Way, Oct 29, 2005
    #2
    1. Advertising

  3. stephg

    stephg Guest

    Thanks for your answer. But unfortunately this change doesn't do the job
    either.
    Still the item set as selected is not shown selected in the drop down list
    on the final page. I have enhance my control by
    ===
    Public ReadOnly Property GetSelectedIndex() As Integer
    Get
    Return ddlCountry.SelectedIndex
    End Get
    End Property
    ===
    And if I debug the calling page ddC1.GetSelectedIndex() returns the correct
    value. But still the item is not shown selected in the page.
    What am I missing????

    "Paul Way" wrote:

    > You'll have to excuse my lack of having an IDE on my computer so you won't be
    > able to copy and paste this code...
    >
    >
    > 1)
    >
    > Public WriteOnly Property SetSelectedValue()
    > Set(ByVal Value)
    > bindDDL()
    > Dim i As Integer
    > ddlCountry.SelectedIndex = 0
    > For i = 0 To ddlCountry.Items.Count - 1
    > If ddlCountry.Items(i).Value = Value Then
    > ddlCountry.Items(i).Selected = True 'I do not think you need
    > this line
    > ddlCountry.SelectedIndex = i
    > exit for 'go ahead and exit the for loop for optimization
    > End If
    > Next
    > End Set
    > End Property
    >
    >
    > Also, if that doesn't work then try the EnsureVisible property (I know that
    > is on listviews)
    stephg, Oct 29, 2005
    #3
  4. Hi,

    I'm pretty sure you can't set an item to Selected if another item in the
    list is already selected. Try replacing this line:

    ddlCountry.SelectedIndex = 0

    With this:

    ddlCountry.SelectedItem.Selected = False

    Also you rebind your DDL everytime you set it which you shouldn't need to
    do. But if you do need to for some reason then make sure you call
    ddlCountry.Items.Clear() as the first line of your bindDDL function. Good
    luck! Ken.

    --
    Ken Dopierala Jr.
    For great ASP.Net web hosting try:
    http://www.webhost4life.com/default.asp?refid=Spinlight
    If you sign up under me and need help, email me.

    "stephg" <> wrote in message
    news:...
    > Thanks for your answer. But unfortunately this change doesn't do the job
    > either.
    > Still the item set as selected is not shown selected in the drop down list
    > on the final page. I have enhance my control by
    > ===
    > Public ReadOnly Property GetSelectedIndex() As Integer
    > Get
    > Return ddlCountry.SelectedIndex
    > End Get
    > End Property
    > ===
    > And if I debug the calling page ddC1.GetSelectedIndex() returns the

    correct
    > value. But still the item is not shown selected in the page.
    > What am I missing????
    >
    > "Paul Way" wrote:
    >
    > > You'll have to excuse my lack of having an IDE on my computer so you

    won't be
    > > able to copy and paste this code...
    > >
    > >
    > > 1)
    > >
    > > Public WriteOnly Property SetSelectedValue()
    > > Set(ByVal Value)
    > > bindDDL()
    > > Dim i As Integer
    > > ddlCountry.SelectedIndex = 0
    > > For i = 0 To ddlCountry.Items.Count - 1
    > > If ddlCountry.Items(i).Value = Value Then
    > > ddlCountry.Items(i).Selected = True 'I do not think you need
    > > this line
    > > ddlCountry.SelectedIndex = i
    > > exit for 'go ahead and exit the for loop for optimization
    > > End If
    > > Next
    > > End Set
    > > End Property
    > >
    > >
    > > Also, if that doesn't work then try the EnsureVisible property (I know

    that
    > > is on listviews)
    Ken Dopierala Jr., Oct 30, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marina
    Replies:
    6
    Views:
    13,587
    KROSS
    Jan 21, 2008
  2. =?Utf-8?B?TWVyZGFhZA==?=

    Drop down list does not see the selected item!

    =?Utf-8?B?TWVyZGFhZA==?=, Oct 20, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    11,862
    =?Utf-8?B?TWVyZGFhZA==?=
    Oct 20, 2004
  3. weiwei
    Replies:
    0
    Views:
    1,013
    weiwei
    Jan 5, 2007
  4. Replies:
    4
    Views:
    138
  5. Replies:
    0
    Views:
    174
Loading...

Share This Page