Web User Control Drop Down List does not show selected item

S

stephg

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
......
======
 
P

Paul Way

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

stephg

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????
 
K

Ken Dopierala Jr.

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.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top