2

次のように確認メッセージボックスにクライアントスクリプトを使用しています

 string script = "fadeScript";
                ScriptManager.RegisterClientScriptBlock(this.Page, script.GetType(), "Script", "Closewindow();", true);

Java スクリプト関数:

<script type="text/javascript">
    function Closewindow() {
        var Result = confirm("Are you sure want to delete?");
        alert(Result);
        if (Result == true) {
            document.getElementById('txtConfirmresult').value = Result;//assigning to hidden text box 
            alert(txtConfirmresult.value);
        return true;
        }
    else 
    {
        document.getElementById('txtConfirmresult').value = Result;//assigning to hidden text box 
        alert('BYE');
         return false;
        }
}

</script>

.cs ファイルのスクリプトの戻り値を使用して、true が返された場合にプロシージャを励起したいと考えています。false が返された場合は、その特定のページだけにとどまらなければなりません。これについて親切に助けてください

4

1 に答える 1

2

サーバーへのポストバックに使用でき__doPostBack(target, argument)ます。__EVENTTARGETその後、投稿データでとを評価し__EVENTARGUMENTて、送り返した内容を確認し、ロジックを適切に実行できます。 これは、もう少し詳細な情報を提供するリンクです

簡単な例:

スクリプト/クライアント側:

<script type="text/javascript">
    function Closewindow() {
        var Result = confirm("Are you sure want to delete?");
        alert(Result);
        if (Result == true) {
            var txtConfirmResult = document.getElementById('txtConfirmresult');
            txtConfirmResult.value = Result;//assigning to hidden text box 
            alert(txtConfirmresult.value); //displaying for debug purposes
            __doPostBack( 'txtConfirmresult', Result ); //sending back to server.
        return true;
        }
    else 
    {
        document.getElementById('txtConfirmresult').value = Result;//assigning to hidden text box 
        alert('BYE');
         return false;
        }
}

C#

    protected void Page_Load(object sender, EventArgs e)
    {
        string target =  Request["__EVENTTARGET"];
        string argument = Request["__EVENTARGUMENT"];

        if (target != null && target.Equals( "txtConfirmresult" ) )
        {
             this.DoSomeGreatServerSideProcessing(argument);
        }
    }

少し正確な変数名を追加するように更新されましたが、スクリプトが機能していれば、既存のコードベースで機能するはずです。テキストボックスに が設定されていないtxtConfirmresult場合にのみ機能するため、ID としての使用には細心の注意を払っています。runat="server"その場合、コンテナー階層を示すために ID が先頭に追加されます。

次のように、「コールバック」に適切な名前を付けることをお勧めします。

スクリプト/クライアント側:

<script type="text/javascript">
    function Closewindow() {
        var Result = confirm("Are you sure want to delete?");
        document.getElementById('txtConfirmresult').value = Result;//assigning to hidden text box 
        if (Result) {

            __doPostBack( 'UserConfirmedFormSubmission', "CloseWindow" ); //sending back to server.
            return true;
        }
        else 
        { 
           return false;
        }
}

C#

    protected void Page_Load(object sender, EventArgs e)
    {
        string target = Request["__EVENTTARGET"];
        string argument = Request["__EVENTARGUMENT"];

        if (!string.IsNullOrEmpty(target) && target.Equals("UserConfirmedFormSubmission"))
        {
             if ( !string.IsNullOrEmpty(argument) && argument.equals("CloseWindow"))
             {
                this.HandleUserRequestedCloseWinow();
             }
        }
    }
于 2012-08-02T14:42:20.670 に答える