DropDownList is not displaying items on run time!

JohnSmith

Member
Joined
Mar 7, 2011
Messages
7
Programming Experience
Beginner
Hi please help as the code is all ok and this is a very famous code on the net. This code is working fine on C# but not working in VB.net. I don't know why on run time it is not showing any item in the dropdownlist please help,
VB.NET:
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Imports System.Collections.Generic

Public Partial Class _Default
	Inherits System.Web.UI.Page
	Private regionCountryData As Dictionary(Of String, ListItem())

	Protected Overrides Sub OnInit(e As EventArgs)
		MyBase.OnInit(e)

		regionCountryData = New Dictionary(Of String, ListItem())()

		regionCountryData("Americas") = New ListItem() {New ListItem("USA"), New ListItem("Canada"), New ListItem("Brazil")}

		regionCountryData("EMEA") = New ListItem() {New ListItem("Switzerland"), New ListItem("Dubai"), New ListItem("Nigeria")}

		regionCountryData("Asia Pacific") = New ListItem() {New ListItem("India"), New ListItem("Japan"), New ListItem("Australia")}
	End Sub

	Protected Sub Page_Load(sender As Object, e As EventArgs)
		If Not IsPostBack Then
			RegionList.Items.Add("---Please select---")
			For Each region As String In regionCountryData.Keys
				RegionList.Items.Add(region)
			Next

			CountryList.Items.Add("---Please select---")
		End If
	End Sub

End Class
 

JohnSmith

Member
Joined
Mar 7, 2011
Messages
7
Programming Experience
Beginner
Exactly there is nothing wrong with the code kulrom. But the issue is RegionList.Items.Add("---Please select---") is not displaying anything as well as CountryList.Items.Add("---Please select---"), I have no clues why is this happening as the same code when converted into C# is working fine.
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Actually it works for me too. When i run the project the dropdownlist is populated correctly.
However you should first ADD the Dictionary items and then use Insert method e.g.
RegionList.Items.Insert(0, new ListItem("---Please select---", "0"))
 

JohnSmith

Member
Joined
Mar 7, 2011
Messages
7
Programming Experience
Beginner
well Kulrom! Let me try this also. If this time it will not work then i'll re install Visual Studio as a last option. Let me see if it will populate or not.
 

JohnSmith

Member
Joined
Mar 7, 2011
Messages
7
Programming Experience
Beginner
DropDownList is displaying items on run time! Finally

Hey kulrom finally it worked not with the insert but without insert. This is what I was missing after Load event

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

Now it is working great.
 

Latest posts

Top Bottom