DotDragnet
May 24, 2012, 07:30:31 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: ASP.NET PostBack - Show Javascript Half Way Through Postback  (Read 354 times)
Dom
Hero Member
*****
Posts: 1681


Taster of pasities. Clanger of both pots AND pans.


DominicNeagle
View Profile Awards
« on: October 03, 2011, 02:57:25 PM »

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 1

So 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!
« Last Edit: October 03, 2011, 02:59:46 PM by Dom » Logged

robthedog
Newbie
*
Posts: 7



View Profile Awards
« Reply #1 on: October 04, 2011, 11:33:51 AM »

Not sure about .net but from what I can see in .php it would be something like:

User uploads file -> Server side script checks if the file already exists

if file doesn't exist then it's ok and copy the file to the uploads directory... run whatever success scripts

if the file exists then copy the file to a temporary directory and store the file name in a session variable, then redirect the user to the confirm page where they can either have a js prompt or two options.

Option 1 - Cancel and return to form (could delete the tmp file at this point)
Option 2 - Replace which would just be a script to copy the temp file (file name stored in the session variable) to the uploads directory then delete the tmp file and redirect to the success script...

Very basic example here http://www.robthedog.com/dom




Logged
Dom
Hero Member
*****
Posts: 1681


Taster of pasities. Clanger of both pots AND pans.


DominicNeagle
View Profile Awards
« Reply #2 on: October 04, 2011, 11:42:02 AM »

Awesome, thanks for the tips/demo/code! Yeah I finally gave up and figured I'd do it the way you've demonstrated here. Although with .NET I'll store the file name in a hidden form control, and then use that to find the file again if they choose to overwrite it.

I just tried your demo and it crashed Chrome lol. Not to worry though, I get the basic idea and will get the same thing working for .NET.

Thanks again for the help. smile
Logged

sickpuppy
Site Chimp
Administrator
Hero Member
*****
Posts: 1912



sickpuppysoftwa
View Profile WWW Awards
« Reply #3 on: October 05, 2011, 08:18:26 AM »

You could also do it with an AJAX call to a page or webservice that checks for existing files. If it fails display the message box, otherwise proceed to the upload.
Logged

Dom
Hero Member
*****
Posts: 1681


Taster of pasities. Clanger of both pots AND pans.


DominicNeagle
View Profile Awards
« Reply #4 on: October 05, 2011, 08:30:48 AM »

Yeah I did think about AJAX, although I've not really used it with .NET before so it'd take a bit of learning/testing/building etc. The thing about working here is that they often want things done yesterday, so I rarely get time to learn new things for a project unless it's a necessity. It bugs me, and I do say that they can have things done quickly or they can have things done properly, but they seem to choose 'quickly' every time.
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!