tl;dr - I need my .NET page to run some .NET code when a user clicks a button, then half way through the code, a javascript 'confirm' dialogue box asks the user to click OK or Cancel, and depending on which they click, some more server side .NET code is executed. All of this needs to happen in one post back.Hi all,
I'm in a slight pickle.
I've got a .NET page that allows users to upload a file to our server. When they do that, the page checks that the file has not already been uploaded, and if it has, it needs to prompt the user that the file has already been uploaded and will be overwritten with the new file of they continue.
Originally, I had nearly everything working; the user selected a file and clicked "Upload", after which the page posted back and checked the server for the given filename. If it found that a file with that name was already on the server, it would show a message that said, "Are you sure?", and gave them a Yes and No button.
When they clicked "Yes" however, the page had cleared the upload box due to the postback (I guess file upload controls aren't maintained on postback) and so when my page went to save the file that had been selected by the user, it gave an error saying that no file was selected.
So I thought I'd try using javascript. Now, when the page is posted back and checks if the file has already been uploaded, it prompts the user to ask if they're sure they want to overwrite the existing file with the new one, by showing a javascript 'confirm' dialogue box. This bit works OK, but what I want to happen is that if the user clicks 'cancel', the .NET code stops running and exits the subroutine. If they click 'OK', then the .NET code continues to run, which will then upload the new file.
This doesn't work. What seems to be happening is that all of the server-side code (that checks if the file exists, and then uploads the file) is executed
before the call to the javascript prompt is made. I've drawn a pretty Pain-based picture of what I mean:
Figure 1So basically, the user selects a file and clicks 'Upload', and then the page posts back and the code shown in Figure 1 is executed. The first block of server side code checks of the file exists. If it does, the client side code is run to show the user a confirmation box. At this point, they'll either say yay or nay, and depending which they choose, I want one of the bottom two server side boxes to run.
Anyone got any ideas how best to do this? It doesn't have to be a javascript prompt, but that's the only way I could think of seeing as the page viewstate didn't seem to maintain the upload control after the page had finished posting back.
TIA!