Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am hoping someone can help me with a problem regarding validating a file upload control in asp.net.

I have a reservation system, where you can reserve tickets. it is built in asp.net 4.5 and c#. When you choose the number of tickets you want, you are brought to a page to enter all personal details as well as upload a photo for each passenger. If you choose to buy 3 tickets you will be shown a gridview with 3 rows, each row has text boxes for your details as well as a file upload control for each user, and a button which uploads the file for that user.

My problem is, lets say you are booking for 3 users, you will have 3 file upload controls and 3 file upload buttons. Lets say you select your file for each of the 3 controls but you forget to click the upload button for each user. When the user tries to go to the next page i need to tell them that they forgot to click the upload button on each of the controls. I am already doing this on the server side which works fine, but my problem is, after the page posts back the file upload controls loose their selected files. I know why the file upload controls loose their value, that is fine, my problem is, how do i remind the user to click the upload button using javascript if they have not clicked the button. Please remember the file upload controls are generated in a grdivew so the html id of each control will be different and dynamic.

I assume javascript is needed, or could i use one of the asp.net validators.

Thanks
David
Posted
Comments
Kornfeld Eliyahu Peter 10-Dec-14 16:35pm    
Not clear...
Standard upload control will upload the selected file upon page post-back without further action from the user!

1 solution

You can do as following:

JavaScript
<script>
$(document).ready(function(){
$(':input').bind('change', function() { setConfirmUnload(true); });
$('#btnsavephoto').click(function() { setConfirmUnload(false); });
function setConfirmUnload(on)
            {
                window.onbeforeunload = on ? unloadMessage : null;
            }
            function unloadMessage()
            {
                return ('You have attempted to leave this page. ' +
                        'If you have made any changes to the fields without clicking the Save button, ' +
                        'your changes will be lost.');
            }
});
</script>





I have written this line in above script:
JavaScript
$('#btnsavephoto').click(function() { setConfirmUnload(false); });


It means that warning message should not be displayed when click on upload photo submit button.


You can refer the following link for better understanding:

http://www.4guysfromrolla.com/webtech/100604-1.shtml[^]
 
Share this answer
 
Comments
Ishpreet Kaur 10-Dec-14 23:44pm    
If you have not clicked upload photo button or forgot to upload, then warning message will be displayed that "You have unsaved data. Do you want to continue?". In that way, user will not navigate to other pages if user has forgot to upload photo

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900