我有一个包含名为“LoggedInUserType”的属性的母版页,我想将此属性转储到与jQuery一起使用的客户端代码中。我正在尝试这个:
$("#headerBar .username").append(" <%=this.LoggedInUserType %>");
但它没有显示任何东西!如果相反,我尝试:
$("#headerBar .username").append(" <%="Hello" %>");
工作正常!有什么建议可以解决问题吗?
编辑:我正在尝试从母版页本身访问该属性。也就是说,上面的两个陈述将放在母版页中。
答案 0 :(得分:1)
最简单且不常见的解决方案是将此声明添加到您的ASPX页面,就在&lt; @ Page /&gt;
下面<%@ MasterType VirtualPath="~/myMaster.Master" %>
VirtualPath是主文件的路径。这将允许您使用下面的stronly类型代码:
<%=Master.LoggedInUserType %>
希望这有帮助, 克里斯
修改强>
您是否想要访问Master Page本身后面的MasterPage代码中声明的属性? 要访问您的属性,需要将其声明为public:
public string LoggedInUserType {get; set;}
然后您可以通过输入以下内容来访问它:
<%= LoggedInUserType %>
答案 1 :(得分:0)
由于它没有抛出未找到LoggedInUserType
属性的异常,唯一可能发生的事情是LoggedInUserType
的值为null或空字符串。
自己看看:
$("#headerBar .username").append(" <%= !string.IsNullOrEmpty(this.LoggedInUserType) ? this.LoggedInUserType : "EmptyUserType" %>");
问题是为什么它是空的?这取决于LoggedInUserType
属性的实例化方式和位置。