473,372 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 1391
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Timo | last post by:
I am trying to use the DropDownList_SelectedIndexChanged event on a dropdown which is dynamically populated with different values at runtime, depending on what the user has been doing. The dropdown...
3
by: vitaly.tomilov | last post by:
I'm using an ASP.NET form to display data from my database table, and I'm doing it in the following way: XmlDataDocument doc = new XmlDataDocument(mydataSet); XPathNavigator nav =...
5
by: andchoi | last post by:
I must be the dumbest ASP.NET 2.0 developer, because despite many different attempts, I am unable to resize a content place holder in the master page... Can anyone clue me in? It seems that the...
2
by: J-T | last post by:
I am constructing an HTML text which I need to render in my ASP.NET page. string rssOutput = Posts.listPosts(Constants.WeblogSectionID); rssOutput contains html code and text.Then I create a...
2
by: Brian | last post by:
Hello... I've created a master page in Visual Web Developer and it creates a content place holder. That's fine and all but I can't seem to resize it. Any suggestions? Thanks, Brian
0
by: MasterChief | last post by:
I have a content place holder that is showing the summary of a sale. Is there a way to create a button in the content place holder that the person can click on to print just what is in the content...
1
by: lamuerte451 | last post by:
Hi: I am a newbie to asp.net 2.0 and am building an app with VS 2005 that will allow users to update records via the web. I have created a master page and then have placed Gridview and details...
4
by: darrel | last post by:
I have a DDL list along these lines: item value="1" text="a" item value="2" text="b" item value="3" text="c" item value="2" text="d" item value="2" text="e" item value="1" text="f" item...
2
by: greenMark | last post by:
Hi All, I'm relatively new to ASP.NET and Visual Web Developer 2008. I'm using a Master page with one content place holder. There is a Cascading Style Sheet file which is being refered by the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.