3

変更を保存せずにページを離れようとしている場合にユーザーに通知する必要があるasp.net(フレームワーク4)に取り組んでいます。onbeforeunloadJavaScriptを介して関数を呼び出すことで解決策を見つけました。IEではうまく機能しますが、Firefox、Safari、Chromeでは機能しません。

ここで問題が発生します。私はAJAXポストバックを使用しており、ページを離れる前にユーザーが変更を保存するように求められた場合、更新パネル内のコントロールはサーバーにポストバックしなくなります。たとえば、更新パネルの内側にある保存ボタンを押しても、何も起こりません。

ここにいくつかのコードスニペットがあります

javascript:

window.onbeforeunload = function (e) {
    if (doBeforeUnload() == true) {
        var ev = e || window.event;
        // For IE and Firefox prior to version 4
        if (ev) {
            ev.returnValue = "You have modified the data entry fields since the last time it was saved. If you leave this page, " +
                "any changes will be lost. To save these changes, click Cancel to return to the page, and then Save the data.";
        }
        // For Safari
        return "You have modified the data entry fields since the last time it was saved. If you leave this page, " +
                "any changes will be lost. To save these changes, click Cancel to return to the page, and then Save the data.";

    }
}

ASP.NET:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
    <ContentTemplate>
        <%--no controls inside of this panel will postback to server if user clicks the “Stay on Page” button--%>
    </ContentTemplate>
</asp:UpdatePanel>

ネット上にはたくさんの投稿があるようですが、解決策はありません。非同期ポストバックを使用しない以外に何かアイデアはありますか?

4

1 に答える 1

0

同様の問題を探していたときに、この質問に出くわしました。私は次のことを行うことでこれを解決することができました:

var doOnBeforeUnloadCheck = true;  // global to set in link button
window.onbeforeunload = function (e) {
    if (doOnBeforeUnloadCheck && doBeforeUnload()) {
        // do stuff
    }
    // Make sure to always re-enable check to ensure the event will still trigger
    // where not specified to disable event.
    doOnBeforeUnloadCheck = true;  
}

他のJavaScriptを実行したり、ポストバックを実行したりLinkButtonする前に、グローバルをfalseに設定してください。

<asp:LinkButton ID="yourButton" runat="server"
    OnClientClick="doOnBeforeUnloadCheck = false;" OnClick="yourButton_Click" />

私は次のリンクからの助けを借りてこの解決策に出くわしました:

http://www.jimandkatrin.com/CodeBlog/post/LinkBut​​ton-UpdatePanel-onbeforeunload.aspx

私は自分の答えを見つける前にこの質問を見つけたので、他の誰かが同じ問題を抱えている場合、これは彼らに時間を節約するかもしれません。お役に立てば幸いです。

于 2013-01-17T21:04:44.560 に答える