469,292 Members | 1,310 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

Selecting item from a Place Holder Error


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
Jun 8 '06 #1
5 1289
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.
"stephen" wrote:

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

Jun 8 '06 #2
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

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


"stephen" wrote:

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

Jun 8 '06 #3
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

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:29**********************************@microsof t.com...
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

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


"stephen" wrote:

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

Jun 9 '06 #4
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


"stephen" wrote:
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

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:29**********************************@microsof t.com...
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

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


"stephen" wrote:

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


Jun 9 '06 #5
Thanks Peter,

It worked.....

Stephen
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:E2**********************************@microsof t.com...
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


"stephen" wrote:
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

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in
message
news:29**********************************@microsof t.com...
> 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
>
> --
> Co-founder, Eggheadcafe.com developer portal:
> http://www.eggheadcafe.com
> UnBlog:
> http://petesbloggerama.blogspot.com
>
>
>
>
> "stephen" wrote:
>
>>
>> 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
>>
>>
>>


Jun 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by andchoi | last post: by
2 posts views Thread by J-T | last post: by
2 posts views Thread by Brian | last post: by
reply views Thread by MasterChief | last post: by
1 post views Thread by lamuerte451 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.