views:

1239

answers:

2

I am writing a file upload application. So it contains a keyname in textbox and a fileupload control. A save button is used to save the keyname and the file. My issue is when I select a file in fileupload control and I donot enter any value in keyname textbox and click on save button, a message is shown 'enter a keyname' but the fileupload control is cleared. How can retain the value in the fileupload control?

+2  A: 

You can't retain value in FileUpload control. This is because of security reasons. you can use RequiredFieldValidator for keyname textbox.

Muhammad Kashif Nadeem
Could you please give me a reference regarding the reason in MSDN?
Sauron
These two will help: http://www.csharpuniversity.com/2009/03/05/uploading-files-and-validating-them-using-the-aspnet-fileupload-and-customvalidator-controls/http://forums.asp.net/p/1155617/1896657.aspx
Muhammad Kashif Nadeem
+1  A: 

I don't think you can assign the value to a FileUpload control for security reasons.

Just think if you could, you could put in by default the path to some known system file that has personal info. Then hide the control on a page and have the user submit the page for whatever reason and auto download files without them knowing.

The file upload control is very locked down because there would just be so many ways to exploit it if it were not.

Have you considered not doing a postback to check if the keyname was entered? Do a check via javascript and abort / show a message if no key value was entered. The postback is what is clearing the value so if you prevent the postback from happening, then your problem will go away as you will not get around the file upload clearing after a postback issue.

Kelsey
Could you please give me a reference regarding the reason in MSDN?
Sauron