ASP中的嵌套母版页.NET导致缺少控件

本文关键字:控件 NET 嵌套 母版页 ASP | 更新日期: 2023-09-27 17:50:35

我有以下层次结构:

MainMasterPage:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MainAdmin.master.cs" Inherits="MyWebsite.Admin.MainAdmin" %>
<form id="form1" runat="server">
<div id="MainMenu_Div" runat="server">
    <asp:ContentPlaceHolder ID="MainMenu" runat="server"/>
</div>
<div id="ContentArea_Div" runat="server">
    <asp:ContentPlaceHolder ID="ContentArea" runat="server"/>
</div>
</form>

TemplateMasterPage:

<%@ Master Language="C#" MasterPageFile="~/Admin/MasterPages/MainAdmin.Master" AutoEventWireup="true" CodeBehind="TemplateMasterPage.master.cs" Inherits="MyWebsite.Admin.TemplateMasterPage" %>
<asp:Content ID="ContentArea" ContentPlaceHolderID="ContentArea" runat="server">
    <div id="InputControls_Div" runat="server">
        <asp:ContentPlaceHolder ID="InputControls" runat="server" />
        <br />
        <asp:Button ID="Submit_Btn" runat="server" Text="Submit" 
            onclick="Submit_Btn_Click" />
    </div>

    <div id="AfterSubmission_Div" runat="server" visible="False">
    <asp:Button ID="AnotherBtn" runat="server" CssClass="linkLookingButton" 
        onclick="AnotherBtn_Click" />
    </div>
</asp:Content>

TemplateMasterPage (Code Behind)

public partial class TemplateMasterPage : System.Web.UI.MasterPage
{
    public string BtnText
    {
        get { return AnotherBtn.Text; } //AnotherBtndoesn't exist in the current context
        set { AnotherBtn.Text = value; } //AnotherBtndoesn't exist in the current context
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Submit_Btn_Click(object sender, EventArgs e)
    {
        Submit_Btn.Enabled = false; //Submit_Btn doesn't exist in the current context
        InputControls_Div.Visible = false; //InputControls_Div doesn't exist in the current context
        AfterSubmission_Div.Visible = true; //AfterSubmission_Div doesn't exist in the current context
    }

我在我的Code Behind示例中注释了错误。这是在使用嵌套母版页之前使用的!

ASP中的嵌套母版页.NET导致缺少控件

刚刚根据你的代码片段建立了一个网站。

一些想法:

  • 你的类在代码后面被命名为TemplatePage,但被称为TemplateMasterPage
  • There is no control AnotherBtn。只有Submit_Btn
  • There is no control Btn。只有Submit_Btn
  • 我得到没有错误在InputControls_DivAfterSubmission_Div

所以我只能说,你的编译器是正确的,你必须添加缺失的按钮或重命名代码,使其成功编译。