如何让客户端确认框值以进行服务器端操作?

时间:2012-08-02 14:39:56

标签: c# javascript asp.net telerik

我正在使用客户端脚本确认消息框如下

 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,我只能留在那个特定的页面上。请帮助我这个

1 个答案:

答案 0 :(得分:2)

您可以使用__doPostBack(target, argument)发回服务器。然后,您可以评估帖子数据中的__EVENTTARGET__EVENTARGUMENT,以查看您发回的内容并正确执行逻辑。 Here is a link that provides a little more in depth information

一个简单的例子:

脚本/客户端:

<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();
             }
        }
    }