访问子页面中的 ASP.NET Master Pager 公共属性
我很不好意思在这里问这个问题,因为它显然已经在 StackOverflow 上被重复了好几次。我读过很多东西,包括:
http://msdn.microsoft.com/en -us/library/xxwa0ff0
http://msdn.microsoft.com /en-us/library/c8y19k6h.aspx
我想我已经做了什么那些文章说,但它对我不起作用。
这是我的母版页的顶部,名为“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”。
我做错了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能仍然需要将母版页的代码移至后面的代码,但如果您将其放在后面,
则 <%@ 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.
这将不起作用,因为您已在母版页(而不是代码隐藏)中创建了属性。通常,母版页代码将被动态编译,并且类名(取决于文件名)将在临时程序集中创建。由于您没有在临时程序集中引用此生成的类名,因此会出现错误。正确的方法是为主控文件提供代码隐藏文件。例如,master 将是
并且在代码隐藏文件(MasterNoNews.master.cs)中具有您的属性:
现在第二种语法可以工作,即
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
And in code-behind file (MasterNoNews.master.cs) have your properties:
Now the second syntax would work i.e.