错误名称在当前上下文中不存在

时间:2013-11-01 12:25:37

标签: c# asp.net visual-studio-2008

我在VS2008中将旧的Web应用程序转换为适当的Web应用程序,同时使用命名空间更加明确。

但是,由于这样做,我遇到了一个问题,即我的基页中的某些字段对于页面后面的代码是不可见的。最大的问题当然是Master Page。

我已经复制了母版页,并重新输入了所有manaually重新创建Designer文件的内容,但仍然在后面的代码中我得到了同样的错误:

  

错误1当前名称“lb_Version”不存在   上下文C:\ SourceControl \ JFA Admin   Portal \ Website \ JFA_Admin \ MasterPageSimple.master.cs 26 13 JFA_Admin

母版页如下:

基页:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPageSimple.master.cs" Inherits="JFA_Admin.MasterPageSimple" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>


<!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">
        <title>Welcome to JFA Admin</title>
        <meta name="Author" content="Marcus Culver"/>
        <link href="Default.css" rel="stylesheet" type="text/css" /> 
    </head>
    <body>
        <div id="MasterPageSimpleWrapper" class="wrapper">
            <form id="form1" runat="server">
                <div id="MasterPageSimpleHeader" >
                    <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
                    </cc1:ToolkitScriptManager> 
                    <br /> <br /> 
                    <div style="text-align:center">
                        <asp:Image ID="Image1" runat="server" ImageUrl="Images/JFA_Admin.png" CssClass="" />
                    </div>
                    <br /> <br /> <br /> <br /> <br /> <br />
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                    </asp:ContentPlaceHolder>
                    <br />
                    <br />
                    <br />
                    <asp:Label ID="lb_Version" runat="server"></asp:Label>
                </div>
            </form> 
        </div>
    </body>
</html>

代码背后:

using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace JFA_Admin
{
    public partial class MasterPageSimple : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_Init(object sender, EventArgs e)
        {
            lb_Version.Text = ConfigurationManager.AppSettings["Env"] + ": " + ConfigurationManager.AppSettings["Ver"];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

结果我添加了一个与命名空间同名的页面,这在尝试引用命名空间时导致了冲突。

重命名页面并将标签移回Page_Load(根据Sonar的评论),所有内容似乎都在重新运行。即使有CC!脚本管理器的标签。

基页:

<%@ Master Language="C#" AutoEventWireup="True" Inherits="JFA_Admin.MasterPageSimple" Codebehind="MasterPageSimple.master.cs" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!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">
        <title>Welcome to JFA Admin</title>
        <meta name="Author" content="Marcus Culver"/>
        <link href="Default.css" rel="stylesheet" type="text/css" /> 
    </head>
    <body>
        <div id="MasterPageSimpleWrapper" class="wrapper">
            <form id="form1" runat="server">
                <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />
                <div id="MasterPageSimpleHeader" >
                    <br /> <br /> 
                    <div style="text-align:center">
                        <asp:Image ID="Image1" runat="server" ImageUrl="Images/JFA_Admin.png" CssClass="" />
                    </div>
                    <br /> <br /> <br /> <br /> <br /> <br />
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                    </asp:ContentPlaceHolder>
                    <br />
                    <br />
                    <br />
                    <asp:Label ID="lb_Version" runat="server"></asp:Label>
                </div>
            </form> 
        </div>
    </body>
</html>

代码背后:

using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace JFA_Admin
{
    public partial class MasterPageSimple : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lb_Version.Text = ConfigurationManager.AppSettings["Env"] + ": " + ConfigurationManager.AppSettings["Ver"];
        }
    }
}

感谢SonarGönül