どのように私は確認してからasp.net/javascriptのボタンを無効にすることができます

StackOverflow https://stackoverflow.com/questions/1049327

質問

私はその上に[削除]ボタンを標準ASP.NET 2.0のWebページを持っています。ユーザーは、ユーザー尋ねる確認ダイアログのポップアップ削除ボタンを押したときに私は何を必要とやってのけるする方法を見つけ出すことができないことである「あなたは確信しているの?」。ユーザーはイエスと言うなら、私は、削除ボタンを無効にして、サーバー側のコードdeleteButton_Clickを実行するポストバックを実行したいです。

ここでタグです。

<asp:Button ID="deleteButton" Text="Delete" OnClick="deleteButton_Click" runat="server" />

ここでは、クライアント側のクリックを処理する(jqueryの中)のjavascriptがあります:

var deleteButton = $(input.eq(0));
deleteButton.click( function()
{
    var a = confirm( "Are you sure you want to delete this item?" );
    if ( a == false )
    {
        return false;
    }
    else
    {
        deleteButton.attr( "disabled", "disabled" );
        __doPostBack( deleteButton.attr( "id" ), "" );
    }
} );

期待通りに確認ダイアログが動作し、無効化はあまりにもOKに動作します。フォームはポストバックの罰金を行いますが、それはdeleteButton_Clickイベントハンドラを実行しません。 __doPostBack JavaScriptコードは、ページ上に存在しません。

私はdeleteButtonタグにUseSubmitBehavior =「false」に追加できますが、それは確認ダイアログの答えを無視します。

だから、多分私はここにASP.NETのあまりを求めています。この作品を作るためにどのように任意のアイデア?

おかげで、

クレイグ

役に立ちましたか?

解決 3

フィードバックをありがとう、それはかなり単純な解決策だった。

javascriptの行は次のようになります。

__doPostBack( deleteButton.attr( "name" ), "" );

の代わりに:

__doPostBack( deleteButton.attr( "id" ), "" );

私は「name」属性までは、doPostBack方法を探していたものであると認識していませんでした。

他のヒント

btnSubmit.Attributes.Add("onclick", "if(confirm(\"Are you sure you want to delete this item?\" ){this.disabled=true;" + ClientScript.GetPostBackEventReference(btnSubmit, "").ToString() + "}else{return false;}");

私はそれが非常に有用であることが判明、このStackOverflowの質問を見てみましょう

フォーム送信する上無効]ボタン

...確認ロジックを追加...そしてあなたが設定する必要があります。

私も頭を行って、あなたのための拡張メソッドを書いた、と私:)

public static void ConfirmThenDisableButtonOnClick(this Button buttonControl, string confirmMessage, string clientFunction)
{
    StringBuilder sb = new StringBuilder();

    // If the page has ASP.NET validators on it, this code ensures the
    // page validates before continuing.
    if (buttonControl.CausesValidation && !String.IsNullOrEmpty(buttonControl.ValidationGroup))
    {
        sb.Append("if ( typeof( Page_ClientValidate ) == 'function' ) { ");
        sb.AppendFormat(@"if ( ! Page_ClientValidate('{0}') ) {{ return false; }} }} ", buttonControl.ValidationGroup);
    }

    //pop confirm, if confirm==true, disable button
    sb.AppendFormat("if(confirm('{0}\')){{this.disabled=true;", confirmMessage.Replace("\"","").Replace("'",""));

    // If a secondary JavaScript function has been provided, and if it can be found,
    // call it. Note the name of the JavaScript function to call should be passed without
    // parens.
    if (!String.IsNullOrEmpty(clientFunction))
    {
        sb.AppendFormat("if ( typeof( {0} ) == 'function' ) {{ {0}() }};", clientFunction);
    }

    // GetPostBackEventReference() obtains a reference to a client-side script function 
    // that causes the server to post back to the page (ie this causes the server-side part 
    // of the "click" to be performed).
    sb.Append(buttonControl.Page.ClientScript.GetPostBackEventReference(buttonControl, ""));

    // if confirm==false do nothing
    sb.Append(";}else{return false;}");

    // Add the JavaScript created a code to be executed when the button is clicked.
    buttonControl.Attributes.Add("onclick", sb.ToString());
}

などもチェックするだけで追加の検証:)

むしろジャバスクリプトによってポストバックを行うよりも、あなたは同じことを達成するためにOnClientClick使用することができます。

<asp:Button OnClientClick="if (confirm('Are you sure?')) {this.disabled = true;} else {return false;}" />

それとも、単にボタンを無効にするよりも、javascriptを通じ多くのアクションを実行したい場合、あなたはこのような何かを言うことができます:

<asp:Button OnClientClick="return DisableOnConfirm();" />

あなたはちょうどあなたがASP機能を実行したくない場合は、文は最終的に言うだろうように、return文の形でそれを必ず言葉遣いしているようにする必要があり「return false;」を

のJavascriptます:

deleteButton.disabled = true;

のC#ます:

deleteButton.Enabled = false;

(あなたがポストバックを行いますので、あなたがページを再作成)あなたはおそらくdeleteButton_Clickイベントでサーバー側のボタンを無効にする必要があります。

<asp:Button ID="deleteButton" Text="Delete" OnClick="deleteButton_Click" runat="server" OnClientClick="javascript:return confirm('are you sure blah blah blah ...')" />

そして、コードビハインドでます:

private void deleteButton_Click(object sender, EventArgs e) {
    deleteButton.Enabled = false;
    // and the rest of event handling ...
}

これは、クリックイベントが発射されていないことを少し奇妙です。私はそれはボタンが無効であることによるものであった推測が、ボタンは、サーバーだけではなく、クライアント上で無効になっている場合、通常はそののみ発生したでしょう。あなたがポストバックおよび/または非同期ポストバックをチェックし、ポストバック(この場合はそのあなたのボタン)の目標を決定し、そこからいくつかのメソッドを呼び出すことができ、あなたのPage_Load関数の理想的なソリューションではありませんが。また__doPostBack()関数を使用して引数を渡すことができます。

あなたは、クライアント側のクリックイベントまたはサーバ側のクリックイベントを話していた場合は、

最初は、私はわかりませんでした。私はあなたが明示的にサーバー側のクリックイベントが発射されていないと述べているべきだと思います。

とにかく、試してください。<%@ページEnableEventValidation =「false」に%>あなたのASPXページ上で、それが動作するかどうか(これはデフォルトでtrueに設定されている)を参照してください。私は、この属性作品の核心ザラザラを覚えていないが、この機能は、サーバー側のイベントが正当な場合に解雇されたことを確認します。例えば。さんがあなたのページには、XSSの脆弱性と削除ボタンを呼び出すためにJavaScriptを注入し、ハッカーがあったとしましょう。この属性は、これらの攻撃を防ぐことができます。

$(":submit").click(function ()
{
    $(this).attr("disabled", true); // disable input

    if (confirm("Press OK to submit"))
    {
        __doPostBack(this.id, ""); // if user presses OK; submit
    }
    else
    {
        // If user presses cancel; enable input and stop submit
        $(this).attr("disabled", false);

        return false;
    }
});

私は、このいずれかのボタンから呼び出すことができJS機能することに決めます。

ここで私は、このような既存の.jsファイルやheadタグと別の場所にJavaScriptを移動するだろう、しかし、作品のサンプルです。

<script>
    function confirmPostback(button) {
        if (confirm('Are you sure?')) {
            button.disabled = true;
            __doPostBack(button.name, '')
            return true;
        } else {
            return false;
        }
    }
</script>

<asp:Button ID="TestPostback" runat="server" Text="Test Postback"
        UseSubmitBehavior="false" OnClientClick="return confirmPostback(this)" />
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top