简体   繁体   English

ASP.Net C# 引用 Site.master 上的元素返回:对象引用未设置为对象的实例

[英]ASP.Net C# reference to element on Site.master returns: Object reference not set to an instance of an object

I've found threads solving this issue within Default.aspx and Default.aspx.cs, but applying those solutions to my problem don't fix my issue.我在 Default.aspx 和 Default.aspx.cs 中找到了解决此问题的线程,但将这些解决方案应用于我的问题并不能解决我的问题。 None of them are helpful in this context:在这种情况下,它们都没有帮助:

I'm trying to access an element on my Site.master page within Default.aspx.cs.我正在尝试访问我的 Site.master 页面上 Default.aspx.cs 中的一个元素。 I want to hide this element if a certain condition isn't met.如果不满足某个条件,我想隐藏这个元素。 However, I cannot reference any elements within my Site.master page (either within Default.aspx.cs OR Site.master.cs)但是,我无法引用我的 Site.master 页面中的任何元素(在 Default.aspx.cs 或 Site.master.cs 中)

Code:代码:

Site.master:网站管理员:

<li><a runat="server" id="editLink" href="~/Edit">Edit</a></li>

Default.aspx.cs:默认.aspx.cs:

MasterPage master = this.Master;
Control linkControl = master.FindControl("editLink");
linkControl.Visible = false;

I've also tried:我也试过:

Site.master:网站管理员:

<asp:Panel ID="Panel1" runat="server">
    <li><a runat="server" id="editLink" href="~/Edit">Edit</a></li>
</asp:Panel>

Default.aspx.cs:默认.aspx.cs:

((Panel)Master.FindControl("Panel1")).Visible = false;
//or..
//Panel testPanel = ((Panel)Master.FindControl("Panel1"));
//Panel1.Visible = false;

I don't know where to start.我不知道从哪里开始。 Thanks!谢谢!

EDIT:编辑:

I've tried to apply resolutions from:我尝试应用以下决议:

ASP.NET - Accessing Master Page elements form the Content Page ASP.NET - 从内容页访问母版页元素

What is a NullReferenceException, and how do I fix it? 什么是 NullReferenceException,我该如何解决?

Specifically from the first:具体从第一个:

adding the following to Default.aspx将以下内容添加到 Default.aspx

<%@ MasterType virtualpath="~/Site.master" %>

adding the following to Default.aspx.cs将以下内容添加到 Default.aspx.cs

Panel testPanel = (Panel)Master.FindControl("Panel1");       
testPanel.Visible = true;

The above code is the suggested fix for the first thread, but I get the same error.上面的代码是第一个线程的建议修复,但我得到了同样的错误。 The second thread seemed to suggest I was referencing the element before it was created thus returning the nullreferenceexception error, but I do not believe this is the case.第二个线程似乎暗示我在创建元素之前引用该元素,从而返回 nullreferenceexception 错误,但我不相信是这种情况。 Ultimately, I do not understand enough to utilize those threads.最终,我对利用这些线程的理解不够。

EDIT编辑

Site.master MCVE: Site.master MCVE:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>

<form id="wrapForm" runat="server">
    <asp:ScriptManager runat="server" EnablePageMethods="true"></asp:ScriptManager>
    <asp:ContentPlaceHolder ID="MainContent" runat="server">
      <asp:Panel ID="Panel1" runat="server">Test Text</asp:Panel>
    </asp:ContentPlaceHolder>
</form>

Default.aspx.cs MCVE:默认.aspx.cs MCVE:

protected void Page_Load(object sender, EventArgs e)
{
    Panel testPanel = (Panel)Master.FindControl("Panel1"); 
    testPanel.Visible = true;
}

MasterType <= 根据ASP.NET应该是Reference - 访问母版页元素形成内容页

<%@ Reference virtualpath="~/Site.master" %> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对象引用未设置为对象asp.net的实例,c# - Object reference not set to an instance of an object asp.net , c# C#,ASP.NET - NullReferenceException - 未将对象引用设置为对象的实例 - C#, ASP.NET - NullReferenceException - Object reference not set to an instance of an object C# Object 引用未设置为 object 的实例。 ASP.NET - C# Object reference not set to an instance of an object. ASP.NET ASP.NET Core 2.x - HttpClient api 返回错误“对象引用未设置为对象的实例” - ASP.NET Core 2.x - HttpClient api returns error 'Object reference not set to an instance of an object' 对象引用未设置为对象asp.net的实例, - object reference not set to an instance of an object asp.net, ASP.NET MVC对象引用未设置为对象的实例 - ASP.NET MVC Object reference not set to an instance of an object 未将对象引用设置为ASP.Net Webservice中的对象实例 - Object reference not set to an instance of an object in ASP.Net webservice 未将对象引用设置为使用ASP.NET Project的对象的实例 - Object reference not set to an instance of an object working with ASP.NET Project C#asp.net应用程序,对Site.css的更改似乎不会影响Site.Master - C# asp.net application, changes to Site.css do not seem to affect Site.Master 对象引用未设置为对象asp.net的实例 - object reference not set to an instance of an object asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM