开发者

Using master page properties inside aspx code

开发者 https://www.devze.com 2023-04-10 14:51 出处:网络
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:

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 m开发者_Go百科aster page itself. That is, the two statements above are to be placed in the master page.


The simplest and not commonly known solution, would be to add this declaration to your ASPX page, just below <@ Page />

<%@ MasterType VirtualPath="~/myMaster.Master" %>

Where VirtualPath is the path to your Master File. This will allow you to use stronly typed code below:

<%=Master.LoggedInUserType %>

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:

public string LoggedInUserType {get; set;}

And then you can access it by typing:

<%= LoggedInUserType %>


Since it does not throw exception that LoggedInUserType property is not found, the only thing could happen is that LoggedInUserType has the value of null or empty string.

Check out yourself:

$("#headerBar .username").append(" <%= !string.IsNullOrEmpty(this.LoggedInUserType) ? this.LoggedInUserType : "EmptyUserType" %>");

The question is why is it empty? It depends on how and where LoggedInUserType property is being instantiated.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号