繁体   English   中英

更改页面名称后,内容页面无法访问控件

[英]Content page not able to access controls after changing the name of the page

我在VS2013中有一个Web应用程序项目,其中包含一个Master Page和一些内容页。 我的母版页眉是:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="MyProject.MasterPage" %>

然后,我在解决方案资源管理器中的“母版页”上单击鼠标右键,然后选择“添加内容页”。 这将为我生成一个类似于以下内容的页面:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="True" CodeBehind="WebForm1.aspx.cs" Inherits="MyProject.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
</asp:Content>

然后,我要做的就是在解决方案资源管理器中右键单击此“ WebForm1”,并将名称更改为“ Login”

这会将内容页面中的标题更改为:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="True" CodeBehind="Login.aspx.cs" Inherits="MyProject.WebForm1" %>

看起来不错,但尽管它不再是内容页面的名称,但仍然显示Inherits="MyProject.WebForm1"

所以我想,“好吧,我可以将Inherits="MyProject.WebForm1" Inherits="MyProject.Login"Inherits="MyProject.Login" 。这是个大错误,因为现在我无法在文件后面的代码中引用任何控件,例如TextBox,Label等。我得到以下错误的清单:

The name 'controlname' does not exist in the current context

因此,看来我的页面无法再正确找到控件的类。

我尝试删除Login.aspx.designer.cs ,然后通过转到“ Project -> Convert to web application来重新创建它,但这没有用。

长话短说,尽管我的页面现在称为Login但是如果我使用Inherits="MyProject.WebForm1" ,我的代码也可以正常工作。 我在其他所有页面上也都尝试过,这是同一个故事。

这是某种错误吗? 还是我添加内容页面的方法有问题?

在您背后的代码中最有可能具有以下功能:

namespace MyProject
{
    public class WebForm1

这是您的页面根据网站项目继承的内容,也应该在“ Inhertis”属性中。 请注意,在那里需要完整的类名,包括名称空间。

因此,如果您想重命名页面文件,请确保也重命名该类。 Visual Studio不会自动执行此操作。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM