Object reference not set to an instance of an object.

In the code behind shown (.aspx page included as well), NextLink.Visible = false (Line 16) is giving the following error:

"System.NullReferenceException: Object reference not set to an instance of an object."

I assume that is occuring b/c the LinkButton is inside of the <HeaderTemplate> on a repeater... Is there any way to keep the LinkButtons inside the HeaderTemplate?  If so, please provide code required.

Thanks!
Public Class PagingCodeBehind
 
Inherits Page
 
...Edited...
Protected FirstLink As LinkButton
Protected PreviousLink As LinkButton
Protected NextLink As LinkButton
Protected LastLink As LinkButton
...Edited...
Sub Paging(Optional WhichPage As Integer = 1,Optional 
 
' If current page is the last page, hide the "next" and "last" navigation links
If CurrentPage = Pages Then
	
	NextLink.Visible = false
	LastLink.Visible = false
	
' Otherwise, show the "next" and "last" navigation links and set the page index each will pass when clicked
Else
	
	NextLink.Visible = true
	LastLink.Visible = true
	NextLink.CommandArgument = CurrentPage + 1
	LastLink.CommandArgument = Pages
	
End If
...Edited...
 
End Sub
=======================================
Index page:
<form runat="server">
 
<asp:Repeater
  ID="myRepeater"
  Runat="server"
  OnItemCreated="myRepeater_ItemCreated">
 
<HeaderTemplate>                        
...Edited...
<asp:LinkButton runat="server" ID="NextLink" Text="Next &raquo;" onClick="ChangePage" />
<asp:LinkButton runat="server" ID="LastLink" Text="Last &raquo;" onClick="ChangePage" />
...Edited...
</HeaderTemplate>
                            
<ItemTemplate>
...Edited...
</ItemTemplate>
                        
<FooterTemplate>
</table>
</FooterTemplate>
                        
</asp:Repeater>
</form>

Open in new window

webdevelopAsked:
Who is Participating?
 
grayeConnect With a Mentor Commented:
In your example, you left off the most important part.... the part where you instantiate the LinkButtons (ie NextLink = New LinkButton)
We'd need to see that part before we can diagnose the rest
0
 
TMarkham1Commented:
I've never used a repeater before, but I'm thinking that you will end up with multiple "NextLink" and "LastLink" LinkButtons on your page. They will probably need to be rendered with different IDs otherwise you'll likely get an Invalid PostBack error. Having said that, I don't think you can reference these link buttons directly on the server side. You're probably going to need to iterate through the repeated items, declare a reference variable to the LinkButtons, and set the visibility as you iterate through the repeated items.

Also, I've run into issues using the Visible property sometimes (it actually causes the object not to be rendered in HTML at all). Most of the time, I just apply the "display = hidden" style instead. That way, the item still gets rendered and it is available for client side access if need be later... it's just hidden.
0
 
webdevelopAuthor Commented:
@graye:
So line 8 isn't doing that?  Sorry, those are the only lines that reference NextLink and it worked perfectly before I tried to move the code inside of the headertemplate for UI reasons.

@TMarkham1:
The headertemplate/footertemplate regions don't repeat, just the itemtemplate region.

I appreciate any further help!
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
codingbeaverConnect With a Mentor Commented:
NextLink is defined in HeaderTemplate, I don't think you can directly access it. You will need to use FindControl to find it first, then you can use it.
0
 
TMarkham1Connect With a Mentor Commented:
If the Link buttons don't repeat, you should be able to access them directly by name.

Like graye mentioned, make sure you instantiate the LinkButtons.

Protected NextLink as LinkButton

...is not the same as...

Protected NextLink as New LinkButton
0
 
webdevelopAuthor Commented:
The solution for me was to take the buttons out of the headertemplate, and set the default visibility on each tag I needed control over.  Then, in the code behind I updated code to control visibility on each tag.

As far as I can tell, it can't be done inside the headertemplate.
0
 
codingbeaverCommented:
>>>As far as I can tell, it can't be done inside the headertemplate.
As I said in my last comment.
0
 
webdevelopAuthor Commented:
...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.