简体   繁体   English

如何从asp.net中的代码隐藏运行确认对话框

[英]How to run confirm dialog from codebehind in asp.net

I want to run confirm dialog from code behind. 我想从后面的代码运行确认对话框。 I searched google and what I found is only where message is predefined and where confirm dialog is linked to button onclick event. 我搜索了google,发现的只有预定义的消息以及确认对话框链接到按钮onclick事件的地方。 I can't link my confirm dialog to onclick event, because I need to check the scenario in codebehind and ask user if he wants to continue. 我无法将我的确认对话框链接到onclick事件,因为我需要在代码隐藏中检查场景并询问用户是否要继续。

I have 1 button and about 10 different scenarios (with different questions for each one of them), where I want to ask user, if he wants to continue. 我有一个按钮和大约10个不同的场景(每个场景都有不同的问题),我想问用户是否要继续。 If he press OK, then I want to continue my code, otherwise I want to cancel. 如果他按OK,则我要继续我的代码,否则我要取消。

I create some javascript function 我创建了一些JavaScript函数

<script type="text/javascript">
    function Confirm() {
        var hidden = document.getElementById("<%=HiddenField1.ClientID %>");
        if (confirm("Test?")) {
            hidden.value = "ok";
            window.location.replace("PrijavaIzpit.aspx");
        } else {
            hidden.value = "cancel";
        }
    }
</script>

which I call from code behind 我从后面的代码中调用

Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "Confirm();", true);

But problem is, this code only runs, if I put return after the line where I call javascript function. 但是问题是,如果我在调用javascript函数的行之后放回车return ,则此代码只会运行。

Is it the right idea? 这是正确的主意吗? How should I do that? 我应该怎么做? Is it possible with javascript? JavaScript可能吗?

Vitor is correct, use AJAX to make calls to your ASP.NET codebehind page. Vitor是正确的,请使用AJAX来调用ASP.NET代码隐藏页。 Since you don't have any actual AJAX code above, I'm simply going to point you to Dave Ward's fantastic page where I learned how to do AJAX with jQuery. 由于您上面没有任何实际的AJAX代码,因此,我只是将您指向Dave Ward的精彩页面 ,在那里我学习了如何使用jQuery进行AJAX。 I encourage you to read his ongoing articles about how to use jQuery with ASP.NET, particularly when he talks about using a Data Transfer Object (which is basically a JSON.stringified JavaScript object which gets deserialized on the codebehind's [WebMethod] , work is done, and the callback provides the data you're looking for). 我鼓励您阅读他正在进行的文章,有关如何将jQuery与ASP.NET一起使用,特别是当他谈到使用数据传输对象(基本上是JSON.stringified JavaScript对象,该对象在代码隐藏的[WebMethod]上反序列化时)特别有用。完成后,回调函数就会提供您要查找的数据)。

You don't mention if you're using jQuery or not, but if you are, you can directly adapt his methods, if you're using pure JS, you'll have to figure out how to make the AJAX calls that way. 您没有提到是否使用jQuery,但是如果您使用的是jQuery,则可以直接修改他的方法,如果您使用的是纯JS,则必须弄清楚如何以这种方式进行AJAX调用。 It's not hard, it's just more convoluted. 这并不难,只是更加令人费解。

You may also find this SO answer helpful. 您可能还会发现此解答很有帮助。

Or for a different take on the problem, this SO answer . 或对于问题的另一种看法, 此SO答案

You can't run a confirm dialog from your code behind in a web application scenario. 在Web应用程序场景中,您无法从代码后面运行确认对话框。 In client side, use javascript to make an ajax call to server side to check the scenario, then, on ajax callback you continue interacting with the user.. 在客户端,使用javascript对服务器端进行ajax调用以检查场景,然后在ajax回调中继续与用户进行交互。

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

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