访问子页面中的 ASP.NET Master Pager 公共属性

发布于 2024-09-24 20:44:57 字数 2289 浏览 1 评论 0 原文

我很不好意思在这里问这个问题,因为它显然已经在 StackOverflow 上被重复了好几次。我读过很多东西,包括:

http://msdn.microsoft.com/en -us/library/xxwa0ff0

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

http://www.velocityreviews.com/forums/t110056-cannot-access-strongly-typed-properties-in-master-page.html

我想我已经做了什么那些文章说,但它对我不起作用。

这是我的母版页的顶部,名为“MasterNoNews.master”:

<%@ Master Language="C#" %>
<%@ Import Namespace="MyMediHealth.DataAccess" %>
<%@ Import Namespace="MyMediHealth.DataAccess.Containers" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    public virtual UserContainer CurrentUser;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
            CurrentUser = new MembershipQueries().getUserFromUserIdName(Page.User.Identity.Name);
    }
</script>

这是我的子页面的顶部,名为“MyProfile.aspx”:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterNoNews.master"
AutoEventWireup="true" CodeBehind="MyProfile.aspx.cs" 
Inherits="MyMediHealth.Interface.MyProfile" %>
<%@ MasterType VirtualPath="~/MasterNoNews.master" %>

这是我的子页面上的代码隐藏,名为“MyProfile.aspx.cs”,这不是工作:

using System;
using System.Web.UI;
using MyMediHealth.DataAccess.Containers;
using MyMediHealth.DataAccess;

namespace MyMediHealth.Interface
{
    public partial class MyProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // doesn't work
            UserContainer user = Master.CurrentUser;
            // also doesn't work
            UserContainer user = ((MasterNoNews)this.Master).CurrentUser
        }    
    }    
}

在第一种情况下,VS 告诉我 System.Web.Ui.MasterPage 不包含 CurrentUser 的定义。在第二种情况下,VS 表示找不到类型或命名空间“MasterNoNews”。

我做错了什么?

I'm embarrassed to ask this here because it's clearly been duplicated several times already on StackOverflow. I've read a lot of stuff including:

http://msdn.microsoft.com/en-us/library/xxwa0ff0

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

http://www.velocityreviews.com/forums/t110056-cannot-access-strongly-typed-properties-in-master-page.html

I think I've done exactly what those article say, but it's not working for me.

Here's the top of my master page, named "MasterNoNews.master":

<%@ Master Language="C#" %>
<%@ Import Namespace="MyMediHealth.DataAccess" %>
<%@ Import Namespace="MyMediHealth.DataAccess.Containers" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    public virtual UserContainer CurrentUser;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
            CurrentUser = new MembershipQueries().getUserFromUserIdName(Page.User.Identity.Name);
    }
</script>

Here's the top of my child page, named "MyProfile.aspx":

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterNoNews.master"
AutoEventWireup="true" CodeBehind="MyProfile.aspx.cs" 
Inherits="MyMediHealth.Interface.MyProfile" %>
<%@ MasterType VirtualPath="~/MasterNoNews.master" %>

Here's the code-behind on my child page, named "MyProfile.aspx.cs" that's not working:

using System;
using System.Web.UI;
using MyMediHealth.DataAccess.Containers;
using MyMediHealth.DataAccess;

namespace MyMediHealth.Interface
{
    public partial class MyProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // doesn't work
            UserContainer user = Master.CurrentUser;
            // also doesn't work
            UserContainer user = ((MasterNoNews)this.Master).CurrentUser
        }    
    }    
}

In the first case, VS is telling me System.Web.Ui.MasterPage does not contain a definition for CurrentUser. In the second case, VS says the type or namespace 'MasterNoNews' could not be found.

What am I doing wrong?

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

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

发布评论

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

评论(2

油饼 2024-10-01 20:44:57

您可能仍然需要将母版页的代码移至后面的代码,但如果您将其放在后面,

则 <%@ MasterType VirtualPath="~/MasterPages/Site.master" %>

在 aspx 页面上,您将能够直接访问母版页的公共方法,而无需进行所有转换。像这样:

UserContainer user = Page.Master.CurrentUser

如果您使用来自大量子页面的母版页代码,那么我将创建一个界面并让母版页实现它。

You'll probably still need to move the master page's code to the code behind, but if you put this,

<%@ MasterType VirtualPath="~/MasterPages/Site.master" %>

on the aspx page you'll be able to access the master page's public methods directly without all that casting. Like so:

UserContainer user = Page.Master.CurrentUser

If you're using the master page's code from loads of child pages then I'd create an interface and have the master page implement it.

只是在用心讲痛 2024-10-01 20:44:57

这将不起作用,因为您已在母版页(而不是代码隐藏)中创建了属性。通常,母版页代码将被动态编译,并且类名(取决于文件名)将在临时程序集中创建。由于您没有在临时程序集中引用此生成的类名,因此会出现错误。正确的方法是为主控文件提供代码隐藏文件。例如,master 将是

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterNoNews.master.cs" Inherits="[Your Namespace].MasterNoNews" %>

并且在代码隐藏文件(MasterNoNews.master.cs)中具有您的属性:

namespace [Your Namespace]
{

    public partial class MasterNoNews : System.Web.UI.MasterPage
    {
      public virtual UserContainer CurrentUser;

      protected void Page_Load(object sender, EventArgs e)
      {
        if (Request.IsAuthenticated)
            CurrentUser = new MembershipQueries().getUserFromUserIdName(Page.User.Identity.Name);
      }
    }
}

现在第二种语法可以工作,即

UserContainer user = (([Your Namespace].MasterNoNews)this.Master).CurrentUser

This will not work because you have created properties within Master page (and not code-behind). Typically, master page code would get compiled dynamically and a class name (depending on file name) will be created in a temporary assembly. As you are not reffering to this generated class name within temp assembly, you get error. Correct way will be to have code behind file for the master. For example, master will be

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterNoNews.master.cs" Inherits="[Your Namespace].MasterNoNews" %>

And in code-behind file (MasterNoNews.master.cs) have your properties:

namespace [Your Namespace]
{

    public partial class MasterNoNews : System.Web.UI.MasterPage
    {
      public virtual UserContainer CurrentUser;

      protected void Page_Load(object sender, EventArgs e)
      {
        if (Request.IsAuthenticated)
            CurrentUser = new MembershipQueries().getUserFromUserIdName(Page.User.Identity.Name);
      }
    }
}

Now the second syntax would work i.e.

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