简体   繁体   中英

Maintain value in Fileupload control in asp.net,C#

i am using Fileupload and 3 dropdown control in update panel, 3 dropdown will be post back on dropdown selected index change event (ie like Country,states and city... get the value from db as per country,states and city)

THE PROBLEM IS

While postback the filename path is gone from the file upload control(as expected/ or Default property).

I am converting the file to byte array, to store in the database from file upload control.

How can i retain the value or Is there any way to solve this issue.

Is there any ajax control for file upload or any free controls which retain the value after postback also...?

Or it is possible to set the value to file upload control during postback ?

Thnks in Advance

You can try to persist file upload value in hidden field between async post-backs using asp.net ajax event handlers.

Sys.WebForms.PageRequestManager.instance.add_beginRequest(BeginRequestHandler)
Sys.WebForms.PageRequestManager.instance.add_endRequest(EndRequestHandler)

function BeginRequestHandler(sender, args) {
  var fileUpload = document.getElementById('fileUpload');
  var hiddenUpload = document.getElementById('hiddenUpload');
  hiddenUpload.value = fileUpload.value;
}

function EndRequestHandler(sender, args) {
  var fileUpload = document.getElementById('fileUpload');
  var hiddenUpload = document.getElementById('hiddenUpload');
  fileUpload.value = hiddenUpload.value;
}

On post back you could hide the FileUpload control and show a Literal that displays the value of the file.

Then, if the user wants to change the uploaded file have them click a button and display the FileUpload control again.

This is how gmail does it.

You could wrap the drop down lists in their own update panel (either a separate panel, or a nested panel).

That would sidestep the problem neatly because only the panel with the drop down lists would be re-rendered when their events fire.

You may have to control which triggers cause which kinds of postback for the panels though, and you may have to set the update mode to conditional and manually manage when each panel updates. That depends on how you have the page arranged and all though. Most of the time though, you don't have to do anything special when using multiple panels.

The FileUpload provides a Property for the filename. Just cache it.

UploadedFile.FileName or something like this

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