简体   繁体   English

是/否ASP.Net中的MessageBox

[英]Yes/No MessageBox in ASP.Net

I want to use JavaScript Message-Box in ASP.Net with Yes/No option. 我想在ASP.Net中使用带有“是/否”选项的JavaScript消息框。 The scenario is, when I am calling the JavaScript function on a Client_click event of a button, its working fine. 场景是,当我在按钮的Client_click事件上调用JavaScript函数时,它可以正常工作。 But when I am calling this from code behind (C#), it pops-up after complete execution of the code. 但是,当我从(C#)后面的代码中调用此函数时,它会在代码完全执行后弹出。 Here is what I am trying: 这是我正在尝试的:

    protected void Message_Click(object sender, EventArgs e)
    {
        if (var == 1)
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "open", "Confirm();", true);
        }
        string confirmValue = Request.Form["confirm_value"];


        if (confirmValue == "Yes")
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
        }
        else
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Nothing has been selected')", true);
        }

    }

Even if the condition var == 1 is true, I am not getting the updated value of string confirmValue . 即使条件var == 1为true,我也无法获取字符串ConfirmValue的更新值。 However if I call it on " OnClientClick ", it works fine. 但是,如果我在“ OnClientClick ”上调用它,则可以正常工作。 Have gone through almost all the urls but no luck yet. 遍历了几乎所有网址,但还没有碰运气。

Any help will be greatly appreciated. 任何帮助将不胜感激。

Your code will do not wait for the confirmation value at the server side. 您的代码不会等待服务器端的确认值。 Page.ClientScript.RegisterStartupScript will just register the client script and that moment that methods execution will be completed. Page.ClientScript.RegisterStartupScript将只注册客户端脚本,然后方法执行将完成。 And it will go on with the next line of the code. 它将继续执行代码的下一行。 It is not the responsibility of the server to handle the registered javascript code. 服务器不负责处理已注册的javascript代码。 So if you want to achieve the Yes/NO confirmation and wait to server side use the AjaxModelPopupExtender with the custom code that will give you what you need. 因此,如果要获得Yes/NO确认并等待服务器端,请使用带有自定义代码的AjaxModelPopupExtender ,它将为您提供所需的信息。 Here you can get more info on how to use the ajaxmodel pop up extender . 在这里,您可以获得有关如何使用ajaxmodel弹出扩展器的更多信息

You should not do it as you are doing. 您不应该这样做。 Coder of Code is right. 代码编码员是正确的。 Do client stuff at client side and come to server with some data to process or decision to process. 在客户端进行客户端处理,并带着一些要处理的数据或决定要处理的数据来到服务器。

In your case you just need to write Javascript function and trigger that function when you want. 在您的情况下,您只需要编写Javascript函数并在需要时触发该函数。 If the response is yes then do postback or Ajax. 如果响应为是,则执行回发或Ajax。 If not then just stay on page. 如果没有,那就留在页面上。

Sample javascipt function will be as below- 示例javascipt函数将如下所示-

<script language="javascript" type="text/javasctipt">
function getConfirmation(){
var r = confirm("Press a button!");
   if (r == true) {
      return true;
   } else {
      return false;
 }
}
</script>

You can directly call this in action like - 您可以直接将其称为-

return confirm("Confirm Input");

If you want to use nice custom UI then you can use Ajax Modelpopupextender in Ajax control toolkit. 如果要使用漂亮的自定义UI,则可以在Ajax控件工具箱中使用Ajax Modelpopupextender。

http://www.codeproject.com/Articles/34996/ASP-NET-AJAX-Control-Toolkit-ModalPopupExtender-Co http://www.codeproject.com/Articles/34996/ASP-NET-AJAX-Control-Toolkit-ModalPopupExtender-Co

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM