簡體   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