Click here to Skip to main content
16,021,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 runtime it is not showing any item in the dropdownlist please help.
VB
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
Posted
Updated 3-Jun-11 20:59pm
v2

I have tried your code and its working fine in the vb.net.(Assuming the drop down is available in ASPX) Is the dropdown RegionList is available in your aspx page or are you binding it dynamic ?

If you are creating it dynamic then you can do something like below.

ASPX

<asp:placeholder runat="server" id="plc" xmlns:asp="#unknown" />



Code Behind

VB
Dim RegionList As New DropDownList()
               RegionList.ID = "RegionList1"
               RegionList.Items.Add("---Please select---")
               For Each region As String In regionCountryData.Keys
                   RegionList.Items.Add(region)
               Next

               plc.Controls.Add(RegionList)
 
Share this answer
 
Comments
johnsmithconsultant 10-Jun-11 9:50am    
Yes Aragon! RegionList is available in the aspx page and I am not binding it dynamically. Even then the code is running perfectly in C#. But the same code is not displaying anything after pressing F5. When the IE opens up no dropdown list. I am really really exhausted. Don't know why it is behaving like this.
johnsmithconsultant 12-Jun-11 4:07am    
Hey Aragon. 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.
The Above Code is Working..........
 
Share this answer
 
Comments
That's Aragon 4-Jun-11 5:29am    
That's fine. Please don't post comment in the form of solution. Please use "Add comment" link below the solution for further conversation. Like I have added this comment below your response.
johnsmithconsultant 10-Jun-11 9:51am    
No it's not! Getting no display in the dropdown I wonder why!
johnsmithconsultant 12-Jun-11 4:08am    
Hey! 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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900