简体   繁体   中英

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. 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: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. Note the "on client click". If that function returns true, then you standard code behind button (server side) will run.

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. 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. 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.

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. 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. 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. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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