UpdatePanel seems to re-encode characters in the page title?
Asked Answered
G

5

8

I have pages with special characters in the title for proper typography, for example it says Exchange ‘07 Groups" with a proper apostrophe, not a single quote. The HTML entity for the apostrophe is ‘

So, I've found that if I set the page title from VB, the title displays just fine, but as soon as an update panel updates that HTML entity gets re-encoded and displays incorrectly as "Exchange ‘07 Groups"

So here's my code where I simply set the page title, then an update panel, and a button to update it...

<script runat="server">
    Protected Sub Page_Load(...) Handles Me.Load
       Page.Title = "Exchange &#8216;07 Groups"
    End Sub

    Protected Sub uxLnkDoClick(ByVal sender As Object, ByVal e As System.EventArgs)
        uxLitLoaded.Text = "Loaded!"
    End Sub
</script>

<!DOCTYPE html>
<html>
<head runat="server"></head>
<body>
<form id="form1" runat="server">
    <asp:ScriptManager runat="server"></asp:ScriptManager>    
    <asp:UpdatePanel runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:LinkButton runat="server" ID="uxLnkDo" OnClick="uxLnkDoClick" Text="Do Something" />
            <asp:Literal runat="server" ID="uxLitLoaded" />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="uxLnkDo" />
        </Triggers>
    </asp:UpdatePanel>
</form>
</body>
</html>

What can be done about this?

Giannagianni answered 8/9, 2010 at 18:53 Comment(0)
D
1

In your code to set the page title, wrap the text in Server.HtmlDecode:

Page.Title = Server.HtmlDecode("Exchange &#8216;07 Groups")
Devilish answered 7/2, 2011 at 15:54 Comment(0)
U
1

I had the same situation with the SM (service mark, as opposed to TM for trademark) which we did setting the page title with Page.Title = "My Company &#8480"; . It reencoded it upon postback.

What we did is in the page head we statically added it < title >My Company &#8480;< /title >

Worked like a charm.

Upstroke answered 25/5, 2011 at 19:6 Comment(0)
R
0

The reason it displays it incorrectly is because .Net is attempting to be safe and HTML encode the title (for prevention of the multiple types of attacks that are possible).

In ASP.Net MVC, you can now use the Html.Raw() method. As far as straight ASP.net, I don't know what the method would be.

Runic answered 13/12, 2010 at 16:29 Comment(2)
It still seems odd to me though because the page title isn't part of the update panel, so why is .NET even looking at things that aren't submitted and changing their values? It doesn't seem to be bothered by it on the initial page load.Giannagianni
Well, the way update panel actually works is that it actually performs a full page refresh on the server - it just uses AJAX effects to only load the part that you want it to load. I'm not entirely sure as to why, but my guess would be that since the title is part of the same script manager as uxLnkDoClick, it's executing it, and subsequently refreshing it on the client side.Runic
W
0

add this check

if(!Page.IsPostBack)
{
Page.Title = "Exchange &#8216;07 Groups"
}
Wrens answered 29/12, 2010 at 10:39 Comment(1)
sory i used c# syntax but it will work most probably > If Not Page.IsPostBackWrens
P
0

or you can simply set the title property in html if its not dynamic!

Parabolize answered 31/12, 2010 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.