Selecting item from a Place Holder Error

S

stephen

Hi,

I have a place holder and I have a checkboxlist in it?
why do i get a "System.NullReferenceException: Object reference not set to
an instance of an object." error why I try to get the selected value from
it?

Thanks,
Stephen

here is what I am doing?


Dim chkBoxList As CheckBoxList

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
ShowCheckBoxes()
btn_Select.Text = "Display Selected"

End If
End Sub

Private Sub ShowCheckBoxes()
Dim tString As String = "12px"
chkBoxList = New CheckBoxList
chkBoxList.Items.Add(New ListItem("Is Enabled", "IsEnabled"))
chkBoxList.Items.Add(New ListItem("Is Active", "IsActive"))
chkBoxList.RepeatColumns = 2
chkBoxList.RepeatDirection = RepeatDirection.Horizontal
chkBoxList.Font.Size = FontUnit.Parse(tString)
phCheckButtons.Controls.Add(chkBoxList)

End Sub


Private Sub btn_Select_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn_Select.Click
If chkBoxList.SelectedItem.Text = "Is Enabled" Then
Response.Write("<script language='javascript'> { alert('Check Box
Selected.') }</script>")

End If

End Sub
 
G

Guest

Are you making sure that at least one item is checked? If there is no item
checked then the SelectedItem is null and you will have to check for that
being null as well.
 
G

Guest

Stephen,
anytime you add a control dynamically to an ASP.NET page, if there is a
postback, you must recreate and add the control again, it doesn't
automatically "persist" across page reloads.
Peter
 
S

stephen

Hi Peter and Jeremy,

Thanks for you replies
Actually I did try that option and it did fail,
in my code sample below:
In the click event again i call the "ShowCheckBoxes()" again
but it still gives me the same error

Thanks,
Stephen
 
G

Guest

Move the "ShowCheckBoxes" method call outside of the "If Not IsPostback" block.
What's happening is when you click the button, that is a postback, and since
your ShowCheckboxes call is inside this block, it never recreats the
controls. Sorry, I thought that was clearer than it apparently is.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
S

stephen

Thanks Peter,

It worked.....

Stephen


Peter Bromberg said:
Move the "ShowCheckBoxes" method call outside of the "If Not IsPostback"
block.
What's happening is when you click the button, that is a postback, and
since
your ShowCheckboxes call is inside this block, it never recreats the
controls. Sorry, I thought that was clearer than it apparently is.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top