DropDownList ... What is going on?

S

shapper

Hello,

I created a DropDownList in runtime and added to a panel:

Private Sub ddlCity_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles ddlCity.Init
ddlCity.ID = "ddlCity"
For Each city As City In [Enum].GetValues(GetType(City))
ddlCity.Items.Add(New ListItem(City2String(city,
CurrentCulture), city.ToString))
Next city
End Sub

I am just filling the DropDownList with the values of an Enum.

Now I want to make the selected value of the list equal to the one
save in property of type City in Profile.City:
ddlCity.Items.FindByValue(Profile.City.ToString).Selected = True

I get the error:
System.NullReferenceException: Object reference not set to an instance
of an object.

My Page_Load has the following code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Load
pMyPanel.Controls.Add(ddlCity)
pMyPanel.Controls.Add(tbName)

Response.Write(ddlCity.Items.Count.ToString) >>>>
This gives me 0!

Response.Write(Profile.City.ToString) >>>>
This gives me London
ddlCity.Items.FindByValue(Profile.City.ToString).Selected =
True

tbName.Text = Profile.Name

End Sub

I don't have any problems with the TextBox. Only with the
DropDownList.

Could somebody help me out?

I don't know what else to try.

Thanks,
Miguel
 
S

shapper

Set breakpoint inside ddlCity_Init and see if it gets called as you are
expecting.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net


I created a DropDownList in runtime and added to a panel:
Private Sub ddlCity_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles ddlCity.Init
ddlCity.ID = "ddlCity"
For Each city As City In [Enum].GetValues(GetType(City))
ddlCity.Items.Add(New ListItem(City2String(city,
CurrentCulture), city.ToString))
Next city
End Sub
I am just filling the DropDownList with the values of an Enum.
Now I want to make the selected value of the list equal to the one
save in property of type City in Profile.City:
ddlCity.Items.FindByValue(Profile.City.ToString).Selected = True
I get the error:
System.NullReferenceException: Object reference not set to an instance
of an object.
My Page_Load has the following code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Load
pMyPanel.Controls.Add(ddlCity)
pMyPanel.Controls.Add(tbName)
Response.Write(ddlCity.Items.Count.ToString) >>>>
This gives me 0!
Response.Write(Profile.City.ToString) >>>>
This gives me London
ddlCity.Items.FindByValue(Profile.City.ToString).Selected =
True
tbName.Text = Profile.Name
I don't have any problems with the TextBox. Only with the
DropDownList.
Could somebody help me out?
I don't know what else to try.
Thanks,
Miguel

This is quite strange.

The ddlCity_Init is being called because the Drop Down List is being
filled.
I added the dllCity at Page_Init event.

If I place the code :
ddlCity.Items.FindByValue(Profile.City.ToString).Selected = True

In ddlCity_Load it works. But if I place it in Page_Load it does not
work. Does this makes any sense?

Isn't the control init event called first then the page load event?

Thanks,
Miguel
 
G

Guest

ddlCity_Init event fires when you are adding control to the panel
on Form_Load event. I'm not sure why you need Page_Init
It all works fine:

Private WithEvents ddlCity As New DropDownList
'
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
'
If Me.IsPostBack Then Exit Sub

Me.Panel1.Controls.Add(ddlCity)
Response.Write(ddlCity.Items.Count.ToString)
ddlCity.Items.FindByValue("5").Selected = True

End Sub

Protected Sub ddlCity_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ddlCity.Init
ddlCity.ID = "ddlCity"
' fill with enum ...
For x As Integer = 1 To 10
Me.ddlCity.Items.Add(New ListItem(x.ToString))
Next x
End Sub
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top