【问题标题】:ASP.NET MasterPage only control?ASP.NET MasterPage 只控制?
【发布时间】:2015-08-03 17:05:10
【问题描述】:

如何制作仅添加到 MasterPages 的控件?或者只是我如何知道控件是添加到 MasterPage aspx 还是添加到从 System.Web.UI.Page 继承的普通 WebForm?

我想像这样在母版页中进行控制:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="SkolaControlsWorkings.Site1" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <cc1:MasterPageOnlyControl runat="server" />
    </div>
    </form>
</body>
</html>


但它不应该接受像这样在普通的WebForm中使用

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="SkolaControlsWorkings.WebForm3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Main" runat="server">
    <cc1:MasterPageOnlyControl runat="server" />
</asp:Content>


谢谢。

【问题讨论】:

  • 请提供一些代码,展示您尝试过的内容......不要指望我们制作示例和解决方案
  • 我添加了代码示例,谢谢。
  • 从未做过,但我认为可以通过询问父对象。如果它是一个母版页,那么可以,如果它是其他东西,则抛出异常。
  • 它可能放在其他控件中但仍然在母版页上,我认为直接父级不会有太大帮助。

标签: asp.net


【解决方案1】:

要从网页访问它,您需要参考母版页。

这是你的做法。

母版页

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

<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <uc1:WebUserControl ID="WebUserControl1" runat="server" />

        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">        
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

母版页代码背后

public partial class Dokimes_so_MasterPage : System.Web.UI.MasterPage
{
    // here we expose the control to public
    public Dokimes_so_WebUserControl getCustomControl
    {
        get
        {
            return WebUserControl1;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

基于此母版页的一页

请注意此参考:&lt;%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %&gt; 您需要它,以便页面现在可以成为用户控件的类。

<%@ Page Title="" Language="C#" MasterPageFile="~/Dokimes/so/MasterPage.master" AutoEventWireup="true" CodeFile="GetControl.aspx.cs" Inherits="Dokimes_so_GetControl" %>


<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>

以及背后的代码

public partial class Dokimes_so_GetControl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // first you get the upper "master"
        // then you get the custom control
        // then you access a property inside the custom control
        ((Dokimes_so_MasterPage)Master).getCustomControl.cSetText = "test";
    }
}

和用户控件

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Dokimes_so_WebUserControl" %>

<asp:Literal runat="server" ID="txtTest"></asp:Literal>

以及背后的代码

public partial class Dokimes_so_WebUserControl : System.Web.UI.UserControl
{
    public string cSetText
    {
        set
        {
            txtTest.Text = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

一些类似的答案:
ASP.NET how to access public properties?

【讨论】:

  • 这不是我要问的,首先没关系,谢谢,问题是如果控件用于 WebForm 而不是 MasterPage。
  • @AbdelfattahRagab 您可以使用此代码将其添加到您的问题中。当您看到如何访问控件时,您可以简单地添加一个“if”来查看是否存在。我在这里演示了如何访问控件……剩下的事情并不难。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多