vineth
vineth

Reputation: 883

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 (i.e 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

Upvotes: 3

Views: 13801

Answers (4)

sujata
sujata

Reputation: 36

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.

Upvotes: 1

Stephen M. Redd
Stephen M. Redd

Reputation: 5428

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.

Upvotes: 0

Branislav Abadjimarinov
Branislav Abadjimarinov

Reputation: 5131

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;
}

Upvotes: 2

citronas
citronas

Reputation: 19375

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

UploadedFile.FileName or something like this

Upvotes: -2

Related Questions