ASP.NET 内容 Web 表单 - 占位符中的内容消失

发布于 2024-08-28 09:49:23 字数 1669 浏览 5 评论 0原文

我正在尝试在我的 asp.net 站点中的 body 标记上设置一个类,该站点使用母版页和内容 Web 表单。我只是希望能够通过向内容 Web 表单页面指令添加 bodycssclass 属性(见下文)来做到这一点。

它通过下面的解决方案起作用,但是当我尝试查看 Default.aspx 时,Content1 控件会丢失其内容。 有什么想法吗?


我是这样做的。我有一个包含以下内容的母版页:

<%@ Master Language="C#" ... %>
<html><head>...</head>
<body id=ctlBody runat=server>
 <asp:ContentPlaceHolder ID="cphMain" runat="server" />
</body>
</html>

它的代码隐藏如下:

public partial class Site : MasterPageBase
{
    public override string BodyCssClass
    {
        get { return ctlBody.Attributes["class"]; }
        set { ctlBody.Attributes["class"] = value; }
    }
}

它继承自:

public abstract class MasterPageBase : MasterPage
{
    public abstract string BodyCssClass
    {
        get;
        set;
    }
}

我的 default.aspx 定义为:

<%@ Page Title="..." [master page definition etc..] bodycssclass="home" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphMain" runat="server">
  Some content
</asp:Content>

该文件的代码隐藏如下:

public partial class Default : PageBase { ... }

并且它继承自:

public class PageBase : Page
{
    public string BodyCssClass
    {
        get
        {
            MasterPageBase mpbCurrent = this.Master as MasterPageBase;
            return mpbCurrent.BodyCssClass;
        }
        set
        {
            MasterPageBase mpbCurrent = this.Master as MasterPageBase;
            mpbCurrent.BodyCssClass = value;
        }
    }
}

I'm attempting to set a class on the body tag in my asp.net site which uses a master page and content web forms. I simply want to be able to do this by adding a bodycssclass property (see below) to the content web form page directive.

It works through the solution below but when i attempt to view Default.aspx the Content1 control loses its content. Any ideas why?

Here is how I'm doing it. I have a master page with the following content:

<%@ Master Language="C#" ... %>
<html><head>...</head>
<body id=ctlBody runat=server>
 <asp:ContentPlaceHolder ID="cphMain" runat="server" />
</body>
</html>

it's code behind looks like:

public partial class Site : MasterPageBase
{
    public override string BodyCssClass
    {
        get { return ctlBody.Attributes["class"]; }
        set { ctlBody.Attributes["class"] = value; }
    }
}

it inherits from:

public abstract class MasterPageBase : MasterPage
{
    public abstract string BodyCssClass
    {
        get;
        set;
    }
}

my default.aspx is defined as:

<%@ Page Title="..." [master page definition etc..] bodycssclass="home" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphMain" runat="server">
  Some content
</asp:Content>

the code behind for this file looks like:

public partial class Default : PageBase { ... }

and it inherits from :

public class PageBase : Page
{
    public string BodyCssClass
    {
        get
        {
            MasterPageBase mpbCurrent = this.Master as MasterPageBase;
            return mpbCurrent.BodyCssClass;
        }
        set
        {
            MasterPageBase mpbCurrent = this.Master as MasterPageBase;
            mpbCurrent.BodyCssClass = value;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静被遗忘 2024-09-04 09:49:23

这现在对我有用......

public class PageBase : Page
{
    public string BodyCssClass
    {
        get;
        set;
    }

    protected override void OnPreInit(EventArgs e)
    {
        MasterPageBase mpbCurrent = this.Master as MasterPageBase;
        mpbCurrent.BodyCssClass = BodyCssClass;

        base.OnLoadComplete(e);
    }
}

This works for me now...

public class PageBase : Page
{
    public string BodyCssClass
    {
        get;
        set;
    }

    protected override void OnPreInit(EventArgs e)
    {
        MasterPageBase mpbCurrent = this.Master as MasterPageBase;
        mpbCurrent.BodyCssClass = BodyCssClass;

        base.OnLoadComplete(e);
    }
}
云淡月浅 2024-09-04 09:49:23

您是否尝试过将 MasterType 指令添加到您的内容页面?就像这样:

无论如何我建议这样做。让我们看看这是否对您有帮助...

请参阅 http://msdn.microsoft。 com/en-us/library/ms228274.aspx
http://msdn.microsoft.com/en-us/library/c8y19k6h .aspx

Have you tried adding the MasterType directive to your content page? Like so:

I recommend doing that anyway. Let's see if that helps you...

See http://msdn.microsoft.com/en-us/library/ms228274.aspx
and http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文