Link to home
Start Free TrialLog in
Avatar of webdevelop
webdevelop

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TMarkham1
TMarkham1

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.
Avatar of webdevelop

ASKER

@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!
SOLUTION
Avatar of codingbeaver
codingbeaver
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
>>>As far as I can tell, it can't be done inside the headertemplate.
As I said in my last comment.
...