I have a FileUpload control and a DropDownlist control in an UpdatePanel and when user select a file for the FileUpload control (no upload yet), in the meanwhile the user select an option from the DropDownList control which will cause a postback! Once the page postback, the path selected in the FileUpload control will gone. How can i remain the path in the FileUpload control? File uploading function was working. I hope can remain the path in the FileUpload control during postback.
I have tried the solution below but the "FileUpload1.HasFile" will return false to me.
If Session("FileUpload1") Is Nothing AndAlso Upload.HasFile Then
Session("FileUpload1") = Upload
lblPhotoUploadErr.Text = Upload.FileName
ElseIf Session("FileUpload1") IsNot Nothing AndAlso (Not Upload.HasFile) Then
Upload = DirectCast(Session("FileUpload1"), FileUpload)
lblPhotoUploadErr.Text = Upload.FileName
ElseIf Upload.HasFile Then
Session("FileUpload1") = Upload
lblPhotoUploadErr.Text = Upload.FileName
End If
but the "Upload.HasFile" in the uploading function below will true when it was executed.
Public Sub uploadPhoto()
Dim FileOK As Boolean = False
Dim FileSaved As Boolean = False
Dim CandidateCode As String = Nothing
Dim newFileName As String = Nothing
Dim extension As String = Nothing
Dim fileNameWithoutExt As String = Nothing
If txtCandidateCode.Text.Trim <> "" Then
CandidateCode = txtCandidateCode.Text.Trim
End If
If Upload.HasFile Then
Dim FileExtension As String = Path.GetExtension(Upload.FileName).ToLower
Dim allowedExtensions() As String = {".png", ".jpeg", ".jpg", ".gif"}
Dim i As Integer = 0
Do While (i < allowedExtensions.Length)
If (FileExtension = allowedExtensions(i)) Then
FileOK = True
End If
i = (i + 1)
Loop
End If
If FileOK Then
Try
fileNameWithoutExt = Path.GetFileNameWithoutExtension(Upload.FileName)
extension = Path.GetExtension(Upload.FileName)
newFileName = fileNameWithoutExt + "_" + CandidateCode + extension
Upload.PostedFile.SaveAs((path1 + newFileName))
FileSaved = True
Catch ex As Exception
lblPhotoUploadErr.Text = ("File could not be uploaded." + ex.Message.ToString)
FileSaved = False
End Try
Else
lblPhotoUploadErr.Text = "Cannot accept files of this type."
End If
If FileSaved Then
pnlUpload.Visible = False
imgPhoto.ImageUrl = ("~/images/" + newFileName)
hfPhotoUploadPath.Value = ("~/images/" + newFileName)
hfFileExtension.Value = extension
hfPhotoUploadFileName.Value = fileNameWithoutExt
End If
End Sub
The FileUpload will only keep it's value if you take it out of the UpdatePanel. That way you can still do everything with the DropDownList and its AutoPostBack but the ajax-postback won't refresh the FileUpload causing it to become empty. This way you don't need the postbacktriggers any more.
Put the UpdatePanel
only around the DropDownList
and any controls the postback has to change. If these controls are not next to each other you can use multiple UpdatePanels, the AutoPostBack will refresh all of them (default behavior, you can even change that).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With