Change Page Title in DNN Programatically from Razo

2019-08-09 18:23发布

问题:

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 ?

回答1:

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);


回答2:

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:

This is the only way working for me:

((DotNetNuke.Framework.CDefault)this.Page).Title = "your title";