Change Page Title in DNN Programatically from Razo

2019-08-09 17:59发布

I've searched a lot for "How to change the title programatically" and only got this result which doesn't work, at least in Razor scripts

DotNetNuke.Framework.CDefault tp = (DotNetNuke.Framework.CDefault)this.Page;
tp.Title = "New Title Here";

This doesn't work in razor host scripts, is there any other solutions to change the page title from Razor host script ?

3条回答
SAY GOODBYE
2楼-- · 2019-08-09 18:10

I'd just like to add to Pola's answer with something that will hopefully work "out of the box".

It's hard to find examples that work directly with DNN, so I submit the following;

@{
System.Web.UI.HtmlControls.HtmlMeta objMetaDescription = new System.Web.UI.HtmlControls.HtmlMeta();
objMetaDescription.Name = "DESCRIPTION";
objMetaDescription.Content = "This will be the meta description content";

DotNetNuke.Framework.CDefault tp = (DotNetNuke.Framework.CDefault)Context.CurrentHandler;
tp.FindControl("Head").Controls.Add(objMetaDescription);
}
查看更多
女痞
3楼-- · 2019-08-09 18:12

This is the only way working for me:

((DotNetNuke.Framework.CDefault)this.Page).Title = "your title";
查看更多
Lonely孤独者°
4楼-- · 2019-08-09 18:19

After many trials and merging others codes, I've found the solution

Using this will give you access to the page access, so you can do whatever you want with it, changing title etc.

var pageObj = Context.CurrentHandler as Page;
pageObj.Title = "My New Title for the page";

And this code will give access to the DNN Page, So you can insert controls etc.

DotNetNuke.Framework.CDefault tp = (DotNetNuke.Framework.CDefault)Context.CurrentHandler;
tp.FindControl("Head").Controls.Add(NewControlObj);
查看更多
登录 后发表回答