Web User Control Drop Down List does not show selected item

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Web User Control Drop Down List does not show selected item

    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 Guest

  2. Similar Questions and Discussions

    1. How To Dynamically Set Selected Item in SELECT List inDW Object
      <-- Cross=posted on DW Application Development forum-- > Hi! I hope I can explain this situation halfway decently... I am creating a DW...
    2. Get value of drop down list from one user control to another -- raise event?
      I have tried just about everything that I can understand. I have 2 user controls on a form. One user control has a drop down list, and the 2nd...
    3. Submitting selected List/Menu Item??
      I got Aalphabets A ? Z as links, a Menu with a pre-selected item and a search button The problem is I just cant get to submit the value of the...
    4. How to set selected item in drop-down list in datagrid
      Donald Welker wrote: SelectedValue='<%# DataBinder.Eval(Container.DataItem,"ABCField") %>' -- Jos
    5. Setting 'selected' item in bound drop down list
      You must set the selected index - meaning, you must know the location of the item that needs to be selected in the list. "ctb"...
  3. #2

    Default RE: Web User Control Drop Down List does not show selected item

    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 Guest

  4. #3

    Default RE: Web User Control Drop Down List does not show selected item

    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 Guest

  5. #4

    Default Re: Web User Control Drop Down List does not show selected item

    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:
    [url]http://www.webhost4life.com/default.asp?refid=Spinlight[/url]
    If you sign up under me and need help, email me.

    "stephg" <stephg@discussions.microsoft.com> wrote in message
    news:ECD5ED5E-041C-495A-8266-E44FEA830B41@microsoft.com...
    > 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. Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139