English 中文(简体)
UpdatePanel seems to re-encode characters in the page title?
原标题:

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?

问题回答

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

Page.Title = Server.HtmlDecode("Exchange &#8216;07 Groups")

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.

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.

add this check

if(!Page.IsPostBack)
{
Page.Title = "Exchange &#8216;07 Groups"
}

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签