简体   繁体   中英

Content sensitive title for aspx page

I am working on a Sharepoint project with a lot of different aspx-pages I have to deal with, but my ASP.net knowledge is still limited. Basically the web application's core is based on userdata. Let's keep it simple and just assume that you can select different userdata and the aspx-pages will render the content based on it.

Now it's not always easy to keep track of all the tabs in your browser. As a solution, the browser tab should render the username in its title besides other (static) information. My current solution is to set the title programatically with the help of this post. As a result, this is what I have so far:

The already existing aspx-pages retrieve some static string from a resource file to render the static part of the title.

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
  <asp:Literal runat="server" text="<%$Resources:UserManager.Resource,GeneralPageTitle%>">                
  </asp:Literal>
</asp:Content>

Now it's my part to adjust the title by the selected user content. My code-behind does the neccessary stuff in the Page_Load function

protected void Page_Load(object sender, EventArgs e)
{
    // (...) some stuff happening here

    if (Page.Master != null)
    {
        var web = SPContext.Current.Web;
        var contentPlaceHolder = (ContentPlaceHolder)Page.Master.FindControl("PlaceHolderPageTitle");
        if (contentPlaceHolder == null) return;

        string oldText = String.Empty;
        foreach (var control in contentPlaceHolder.Controls)
        {
            var textControl = control as ITextControl;
            if (textControl != null)
            {
                oldText += textControl.Text;
            }
        }

        contentPlaceHolder.Controls.Clear();

        var literalControl = new LiteralControl();
        var userName = web.GetCurrentUserName();
        literalControl.Text = String.Format("{0} - {1}", oldText, userName);
        contentPlaceHolder.Controls.Add(literalControl);
    }
}

Of course, since there are a lot of different aspx-pages, you would not put this code into each Page_Load function because of code duplicity. But for this example it'll do the trick.

Basically, yes, this works. BUT it's a bit hacky. Furthermore I still don't like the idea of editing all the aspx's code-behind files (there are lots of them). I'd rather prefer a cleaner solution. Is there any way of achieving the same goal differently?

Update

With the help of SazooCat I found out that you could also add code behind to your custom masterpage ( see this ). Well, that works, too. And I only have to edit a single file. Is this good practice or a NoGo?

You know you can do (in code behind) Page.Title = "My custom title string" right?

Oh, and if it is a common construction of some properties, if you make a class

public class MyCustomBasePage Inherits Page

Then in THERE put in the Page.Title = ??? bit in an OnLoad event

Then each of your content pages can inherit from MyCustomBasePage eg

Public class MyExamplecontentPage Inherits MyCustomBasePage

Following up from comments... if you're setting a default title at master page level and an override title and content page level then you need to read this other entry: How to set Page's Title from a web content page in ASP.NET 3.5

This is the route I am going now, because it fits my needs the best.

First I created a custom User Control

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BrowserTabInfo.ascx.cs" Inherits="Controls.BrowserTabInfo" %>
<asp:Literal runat="server" ID="userName"></asp:Literal>

that executes some code-behind.

protected void Page_Load(object sender, EventArgs e)
{
    var web = SPContext.Current.Web;
    if (web == null) return;

    var userName = web.GetCurrentUserName();
    userName.Text = "- " + userName;
}

And then the User Control is inserted into the master page.

<title id="onetidTitle"><asp:ContentPlaceHolder id="PlaceHolderPageTitle" runat="server"/><browsertabinfo:BrowserTabInfoControl id="browsertabinfo" runat="server"/></title>

Even if there will be a different master page someday, you just have to insert the user control into that master page again to get the same functionality. And you only have to touch (basically) one file instead of dozens of content pages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM