我一直在尝试链接一个 Web 表单,以便能够访问母版页中的变量。我在它起作用之前就这样做了。但现在当我这样做时,我得到了一个错误。
Site.Master 中的代码
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" %>
Site.Master.cs中的代码
public partial class Site : System.Web.UI.MasterPage
{
public string hi = "";
protected void Page_Load(object sender, EventArgs e)
{
}
}
WebForm1.aspx中的代码:
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs" Inherits="WebForm1" %>
<%@ MasterType VirtualPath="~/Site.master" %>
WebForm1.aspx.cs中的代码
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Master.hi="new"
}
}
当我将鼠标悬停在 hi 上时,我收到此消息
Error in Visual Studio - "You can use navigation bar to switch contexts
这是另一张图片 Screenshot
如果你们能帮助我,那就太好了
最佳答案
我在 MVC 应用程序中遇到了同样的问题,因此 WebForm-CodeFile-CodeBehind 建议并不适用。
但是,我意识到我创建的新类的“Build Action”设置为“Content”。一旦我将其更改为“编译”,一切又开始工作了。
关于c# - Visual Studio 上的奇怪错误 - "you could use the navigation bar to switch context",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34193013/