简体   繁体   English

有没有办法通过从后面的代码调用函数在 ASP.Net VB 中创建是/否弹出窗口?

[英]Is there a way to create a Yes/No popup in ASP.Net VB by calling a function from the code behind?

I need to check a variable in the code behind of a ASP.Net web page and if the variable is a certain value I want to display a modal popup with a message and a Yes/No button.我需要检查 ASP.Net 网页后面代码中的变量,如果变量是某个值,我想显示一个带有消息和是/否按钮的模式弹出窗口。 The message will be sent from the code behind.消息将从后面的代码发送。

You can't really do this.你真的不能这样做。 However, you can get much the same effect.但是,您可以获得大致相同的效果。

First approach - is very easy第一种方法 - 非常简单

This assumes the user going to click on a button on the form.这假设用户要单击表单上的按钮。 So, say a delete button, or some action to say run a process.所以,说一个删除按钮,或者说运行一个进程的一些动作。

You drop in your standard asp.net button onto your form like this:你把你的标准 asp.net 按钮放到你的表单上,如下所示:

    <asp:Button ID="Button3" OnClientClick="return myconfirm();"
        runat="server" Text="Delete record" Width="90px" />
    <script>
        function myconfirm() {

            var x = confirm("Do you want to delete this record");
            return x;
        }
    </script>

So the above will FIRST run the "js" script code.所以上面将首先运行“js”脚本代码。 Note the "on client click".注意“在客户端点击”。 If that function returns true, then you standard code behind button (server side) will run.如果该函数返回 true,则按钮(服务器端)背后的标准代码将运行。

In fact, in the vast number of cases, the user "has" to click on something for action to take place.事实上,在大多数情况下,用户“必须”点击某物才能执行操作。 You really say click on a button, then code behind starts running.你真的说点击一个按钮,然后背后的代码开始运行。 However during this so called "round trip", you can't have your code STOP and ask for input such as yes/no.但是,在这个所谓的“往返”期间,您不能让代码停止并要求输入,例如是/否。

Why?为什么? Because when you click a button, the web page travels up to server.因为当您单击一个按钮时,网页会传送到服务器。 Your code behind starts running.你背后的代码开始运行。 Any thing you change on the web page is now changed with the code behind.您在网页上更改的任何内容现在都随背后的代码而更改。 And THEN the whole page is sent back to the browser.然后整个页面被发送回浏览器。 If you stop or interrupt the code while in code behind, then the web page NEVER will get sent back down to the browser until ALL OF your code behind is 100% done, and is finished.如果您在代码隐藏时停止或中断代码,则网页永远不会被发送回浏览器,直到您隐藏的所有代码都 100% 完成并完成。 When all that code behind is all done, THEN the page gets sent back down to the browser.当所有后面的代码都完成后,页面就会被发送回浏览器。 So you can't interrupt the code.所以你不能中断代码。

There are a few other ways to pop up a dialog.还有其他几种弹出对话框的方法。 And you CAN have a dialog pop and launch as a result of code behind, but it will in effect be the LAST thing your code does, and this will mean that a script block is send down along for the rid in the web page also being sent down to client side.并且由于代码隐藏,您可以弹出并启动对话框,但这实际上是您的代码所做的最后一件事,这将意味着一个脚本块被发送到网页中也被删除发送到客户端。

And in place of the "lame" confirm() in js, you can certainly use say jQuery.UI which allows you to make some half decent dialog boxes compared to the horrid and ugly and simple confirm dialog box.代替 js 中的“蹩脚”confirm(),你当然可以使用 say jQuery.UI,与可怕、丑陋和简单的确认对话框相比,它允许你制作一些半体面的对话框。 To be fair, the Chrome browser confirm() does look quite nice, but confirm()/alert() dialogs in most browsers leaves a lot to be desired, and calling them "ugly" is being kind.公平地说,Chrome 浏览器的 confirm() 确实看起来不错,但是大多数浏览器中的 confirm()/alert() 对话框还有很多不足之处,称它们为“丑陋”是一种善意。

The other way to ask/get prompts?询问/获取提示的另一种方式? Don't use a confirm/dialog.不要使用确认/对话框。 Simply put your yes/no on the form as say radio buttons.只需将您的是/否放在表单上,​​就像单选按钮一样。 If you have 2-3 questions, then simply set auto-post back = true, and after the first yes/no (say a radio button group), then a post back occurs, and you display the next question.如果您有 2-3 个问题,那么只需设置 auto-post back = true,然后在第一个是/否(例如单选按钮组)之后进行回发,然后显示下一个问题。 This works VERY well I find with code behind.这对我发现背后的代码非常有效。

Last but not least?最后但并非最不重要的? You can certainly have code behind pop up a dialog box if it is the LAST thing or LAST line of your code.如果它是代码的最后一件事或最后一行,您当然可以让代码背后弹出一个对话框。 You can do a registerScipt block, and it can popup a dialog for yes/no.你可以做一个 registerScipt 块,它可以弹出一个是/否对话框。 But since it is only the VERY last code you can execute in your code behind, then your code behind in 99% of cases is going to be the result of a user having clicked on a asp.net button.但是,由于它只是您可以在后面的代码中执行的最后一段代码,因此在 99% 的情况下,您的后面的代码将是用户单击 asp.net 按钮的结果。 Since that is the case, then the above first example to ask/confirm to run the button code again will suffice in 99% of the cases.既然是这样,那么上面的第一个示例要求/确认再次运行按钮代码就足以满足 99% 的情况。 So just keep in mind that you can't have ANY blocking code in code behind during that round trip.所以请记住,在往返过程中,您不能在代码中隐藏任何阻塞代码。 As a result, you can't have a blocking prompt appear and THEN the code behind continues.因此,您不能出现阻塞提示,然后后面的代码会继续。 The code behind has one shot, one change to run.后面的代码只需要一次,一次更改就可以运行。 That code runs while the page is up on the server.该代码在页面在服务器上启动时运行。 Your code runs, changes things, values of controls, and then once done the page travels back to the client side.您的代码运行、更改内容、控件值,然后一旦完成,页面将返回客户端。 This is the so called round trip.这就是所谓的往返。

As noted, you can certainy in that round trip setup the browser to prompt with a yes/no dialog AFTER the page travels back to the browser.如上所述,您可以在往返设置中确定浏览器在页面返回浏览器后提示是/否对话框。 So for example, I do say setup a toast message.例如,我确实说设置了一个吐司消息。 If a person say clicks on some button, and say to do something (say download a file), and the file does not exist?如果一个人说点击了某个按钮,然后说要做某事(比如下载一个文件),而该文件不存在? I can have the browser say spit out a toast message.我可以让浏览器说吐出一条吐司消息。 So you can in code behind trigger a dialog box, or toast messages and even have a dialog box pop up from the code behind, but just keep in mind that setup of the toast message or popping up of a dialog box will be the LAST action you do in code behind, and then the browser travels back to client, is displayed, and THEN your dialog box pops up.所以你可以在后面的代码中触发一个对话框,或者 toast 消息,甚至可以从后面的代码中弹出一个对话框,但请记住,toast 消息的设置或弹出一个对话框将是最后的操作你在后面的代码中做,然后浏览器返回到客户端,显示出来,然后你的对话框弹出。 You can (and will) then have code behind (and another round trip) occur as a result of that dialog box popping up.由于该对话框弹出,您可以(并且将会)然后将代码隐藏(和另一个往返)发生。 I can post an example of how to do this, but in this exmaple while code behind is setting up and triggering a dialog box, there will be no code blocking, or waiting for a yes/no and then the code block continues.我可以发布一个如何执行此操作的示例,但在此示例中,当隐藏代码正在设置和触发对话框时,不会有代码阻塞,或者等待是/否,然后代码块继续。

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

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