I have a Master Page with nested pages in ASP.NET. Fairly simply I want to databind some hyperlinks to a gridview for the master page. This however is throwing a null reference exception when I try to set the DataSource.
- I tested the code on a separate page and it works as expected.
- When debugging I can see the Page_Load Sub hit twice, I don’t know if this is the cause
- I have one nested page being loaded, this is a blank page at the moment
EXCEPTION
Object reference not set to an instance of an object.

MASTERPAGE CODE BEHIND
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Using model = New AccessDataModelDataContext
Dim result = From x In model.SiteLinks
Where x.IsActive
Select x
SiteLinks.DataSource = result
SiteLinks.DataBind()
End Using
End If
End Sub
MASTERPAGE ASPX CODE
<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Site.master.vb" Inherits="Access.Manager.Site" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="~/Styles/Styles.css" rel="stylesheet" type="text/css" />
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
Site.Master
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<div class="navigation">
Site Links
<asp:GridView ID="SiteLinks" runat="server">
</asp:GridView>
</div>
<div class="content">Site Content</div>
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
One glaring issue that I see, and could be your problem, is that your GridView exists inside of your content placeholder. Generally, those placeholders are for pages to implement, not the masterpage itself.
Move the contents of the place holder out of it: