在 aspx 代码中使用母版页属性
我有一个母版页,其中包含一个名为“LoggedInUserType”的属性,我想将此属性转储到客户端代码中以与 jQuery 一起使用。我正在尝试这个:
$("#headerBar .username").append(" <%=this.LoggedInUserType %>");
但它没有显示任何内容!如果我尝试:
$("#headerBar .username").append(" <%="Hello" %>");
效果很好!有什么建议可能是什么问题吗?
编辑:我正在尝试从母版页本身访问该属性。即上面两条语句要放在母版页中。
I have a master page which contains a property called "LoggedInUserType" and I want to dump this property into the client code to be used with jQuery. I am trying this:
$("#headerBar .username").append(" <%=this.LoggedInUserType %>");
But it is not showing anything! If instead I try:
$("#headerBar .username").append(" <%="Hello" %>");
It works fine! Any suggestion what the problem could be?
EDIT: I am trying to access the property from the master page itself. That is, the two statements above are to be placed in the master page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单且不为人所知的解决方案是将此声明添加到您的 ASPX 页面中,位于 <@ Page /> 下方。
其中 VirtualPath 是主文件的路径。这将允许您使用下面的强类型代码:
希望这有帮助,
Kris
编辑:
您说您想访问母版页本身后面的母版页代码中声明的属性?
要访问您的财产,需要将其声明为公共:
然后您可以通过键入以下内容来访问它:
The simplest and not commonly known solution, would be to add this declaration to your ASPX page, just below <@ Page />
Where VirtualPath is the path to your Master File. This will allow you to use stronly typed code below:
Hope this helps,
Kris
EDIT:
You say you want to access a Property declared in MasterPage code behind on Master Page itself?
To access your property it needs to be declared as public:
And then you can access it by typing:
由于它不会抛出未找到
LoggedInUserType
属性的异常,因此唯一可能发生的情况是LoggedInUserType
的值为 null 或空字符串。检查一下自己:
问题是为什么它是空的?这取决于
LoggedInUserType
属性的实例化方式和位置。Since it does not throw exception that
LoggedInUserType
property is not found, the only thing could happen is thatLoggedInUserType
has the value of null or empty string.Check out yourself:
The question is why is it empty? It depends on how and where
LoggedInUserType
property is being instantiated.