我在ASP.NET中有一个包含嵌套页面的母版页。相当简单,我想将一些超链接数据绑定到母版页的gridview。但是,当我尝试设置DataSource时,这会引发空引用异常。
异常
Object reference not set to an instance of an object.
MASTERPAGE CODE背后
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代码
<%@ 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>
答案 0 :(得分:1)
我看到一个明显的问题,也就是你的问题,就是你的GridView存在于你的内容占位符中。通常,这些占位符用于实现页面,而不是主页本身。
将占位符的内容移出:
<body>
<form id="form1" runat="server">
<div>
Site.Master
<div class="navigation">
Site Links
<asp:GridView ID="SiteLinks" runat="server">
</asp:GridView>
</div>
<div class="content">Site Content</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
答案 1 :(得分:1)
我们可以像这样从内容页面将数据绑定到母版Gridview,
GridView grid_data = this.Master.FindControl("show_zone_data") as GridView;
grid_data.DataSource = dt;
grid_data.DataBind();