CurrentPage.PageName in a unit



Please can someone give me some input on <%= CurrentPage.PageName %> (for example) in an ascx file. Is it supose to work? For me, some times it does and some times it doesnt. I find it very strange. At home in my development enviroment it works fine for instance to write <title>My site - <%= CurrentPage.PageName %></title>. But at some instances here at work, it doesnt work. What am I missing?

Also, it would be nice to know: what is the difference between <%= CurrentPage.PageName %> and <% CurrentPage ["PageName"] %>?


Many thanks!

Nov 15, 2011 8:36

Are all the user controls you are testing derived from EPiServer.UserControlBase? If you create a default user control in visual studio it will derive from System.Web.UI.UserControl which does not define CurrentPage.

Nov 15, 2011 10:12

You need to inherit from EPiServer.UserControlBase to user CurrentPage in a usercontrol.

And I don't think there is any difference between using CurrentPage.PageName vs CurrentPage["PageName"], it's just more beautiful with .PageName. :)

Nov 15, 2011 10:13

And for your second question: There's no difference. PageData.PageName returns (((string) this["PageName"]) ?? string.Empty);

Nov 15, 2011 10:14

That's a real race condition Erik, I beat you and you beat me :)

Nov 15, 2011 10:15

Ah ok, I get it. Ok, anything negative with deriving from EPiServer.UserControlBase?

Nov 15, 2011 10:51

Not really, other than you can't reuse the UserControl on a page that does not inherit from PageBase. But if you want to use CurrentPage that is what you'll do anyway. If you want your control to be more generic (use it outside EPi projects etc) you must have some other way of storing the "Page name", e.g. a property on the UserControl that you set from the page.

Nov 15, 2011 11:00

Ok ok. I will put some more thoughts into it at home later when Iam not so busy. Probebly will have some more questions then. =)

Many many thanks for now. Very helpful. I try to juggle various work tasks and study .net at home.

Nov 15, 2011 11:13

Magnus: ok, this might be a silly question, but how do I see if a page inherits from PageBase? In the code behind for an aspx file it says "public partial class StandardPage : EPiServer.TemplatePage" or "public partial class WebForm1 : System.Web.UI.Page".

Iam guessing that the first one inherits from PageBase but not the later right? CurrentPage didnt seem to work in the later.

Nov 16, 2011 0:44

Correct, TemplatePage inherits EditPage which inherits SimplePage which inherits PageBase :) In Visual Studio, right click TemplatePage in your class definition and select go to definition which will open up the object explorer where you can continue to explore the inheritance.

Nov 16, 2011 7:56

Ok, thank you, both of you. Great help.

Nov 16, 2011 9:11
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.