UpdatePanel asyncpostback後にタイトルを失うページ
-
06-07-2019 - |
質問
最近、ページタイトルが標準の<!> quot; Untitled Page <!> quot;にリセットされることに気付きました。メインページのUpdatePanel
内からasyncpostbackを実行した後。マスターページ内からのポストバック中(マスターページ内の検索ボックスボタンをクリックしたときなど)にタイトルは失われません。
ドキュメントのタイトルを設定するために別のcontentplaceholder
を使用することで、このような問題を回避できると想定しましたが、明らかに間違っていました。 ASPXページのコードビハインドに明示的にタイトルを設定する必要がある以外に、私が見逃しているものが他にありますか(以下のセットアップ方法で回避したいと思っていました)?
これは、マスターページ(以下のマスターページコード)を呼び出しているページの基本的な要点です。
<asp:Content ID="Content1" ContentPlaceHolderID="title" Runat="Server">
Page Title
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
//random javascript validators
</script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="content" Runat="Server">
<div class="title">
Account Management
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
//Username + Password Set Form
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
これはマスターページのものです。 ASP.NET AJAX ScriptManagerは、本文の<form>
タグの後に最初に配置されます。
<head id="Head1" runat="server">
<title>
<asp:ContentPlaceHolder id="title" runat="server">
</asp:ContentPlaceHolder>
</title>
//Stylesheet references
<script type="text/javascript">
//Random javascript functions
</script>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
解決
コンテンツページのTitleプロパティを使用することに反対ですか?
<%@ Page Title="Your Page Title" Language="vb" AutoEventWireup="false" MasterPageFile="~/MasterPages/...
ページの読み込み時にプログラムからアクセスすることもできます...
他のヒント
サイトの1つでこの問題に遭遇しました。
即時修正は、page_loadメソッドの背後にあるマスターページのコードのタイトルをリセットすることでした。
明らかに、ajax呼び出しが発生すると、マスターページが再実行されます。これにより、タイトルが消えていました。
例:
protected void Page_Load(object sender, EventArgs e) {
this.Page.Title = "whatever title you have...";
}
より良い修正方法は、MS updatepanel crapをドロップして、実際に呼び出しを適切に制御できるJSON / jQueryの使用を開始することです。
次のようなタイトルタグのスペースを削除すると回避できる奇妙なバグです。
<title><asp:ContentPlaceHolder id="title" runat="server"></asp:ContentPlaceHolder></title>
Sharepoint 2010でテスト済み
サーバー側のコードを変更するのではなく、JSで修正するだけではどうですか:
$(function(){
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (!(prm)) return;
document.orginalTitle=document.title;
prm.add_endRequest(function(s, e){
if (document.title.replace(/\s/g,"").length==0)
document.title=document.orginalTitle;
});
});
タイトルをプログラムで設定すると、PostBackでない場合にのみ発生します。セーブバック/ロードポストバックメソッドを書き換えて、ビューステートバッグにタイトルを保持するだけです。
protected override void LoadViewState(object savedState)
{
object[] allStates = (object[])savedState;
if (allStates[0] != null)
base.LoadViewState(allStates[0]);
if (allStates[1] != null)
Page.Title = (string)allStates[1];
}
protected override object SaveViewState()
{
object[] allStates = new object[2];
object baseState = base.SaveViewState();
string pageTitle = Page.Title;
allStates[0] = baseState;
allStates[1] = pageTitle;
return allStates;
}
Viewstateにページタイトルを配置し、ボタンポストバックClickイベントの文字列を取得して、Page.Titleに割り当てることができます
public string MyPageTitle
{
get
{
return (string)ViewState["MyPageTitle"];
}
set
{
ViewState["MyPageTitle"] = value;
}
}
On page load assign:MyPageTitle = <!> quot; My Cool Web Page Title <!> quot ;; 次に、ボタンクリックイベントで:
protected void MyLinkButton_Click(object sender, EventArgs e)
{
Page.Title = MyPageTitle;
}