File upload filename still getting cleared!
Ok - we got a file upload control
Code:
<div style="text-align:center; vertical-align:middle"><asp:FileUpload ID="SourceFileUpload" EnableViewState="true" runat="server" /></div>
It's got EnableViewState as true.
And then this checkbox
Code:
<asp:CheckBox ID="StandardCheckBox" AutoPostBack="true" Text="Yes" EnableViewState="true" runat="server" /></div>
And then this sub
Code:
Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StandardCheckBox.CheckedChanged
'FindControl("TransDead").Visible = False
If StandardCheckBox.Checked Then
TranslationDeadline.Text = "Standard Turnaround"
Else
If TranslationDeadline.Text = "Standard Turnaround" Then
TranslationDeadline.Text = ""
End If
End If
End Sub
If you browse for a filename prior to click this check box then when you click the check box the filename gets cleared.
What is causing that?
Re: File upload filename still getting cleared!
I should have mentioned that all of this is in an AJAX update panel.
Is there a way to take the browser control out of the panel?
Re: File upload filename still getting cleared!
I have a feeling this is happening because you can't set a file upload HTML control text value from code, and I would imagine that includes viewstate as well.
It is a security feature.
Imagine you went to a website, and that site had a file upload component on it, and when you went to the website, it autofills in some important personal file from your computer into the textbox, and then submits the form via javascript. That would be a HUGE security flaw in the browser and would be exploited to no end.
So as it stands, a value of an upload control should only be able to be set by the user. If a post back occurs, the value is lost when the page is reloaded. The code may even be trying to set the value back, but I believe the underlying HTML control that is being rendered in the end will simply not accept a value being set.
Re: File upload filename still getting cleared!
We kind of thought that - thus why we asked how to get the browse control out of the update panel.
The problem we have is that the user must satisfy several selection before browsing and uploading.
If they browse and then decide they really wanted to set a date as x/y/z then when they do such it clears the control.
The only way around this otherwise would be to make all those "selection" items be JS and keep them client side - right?
Re: File upload filename still getting cleared!
Or disable the actual file selection until all selections have been made.
Make it sort of like a 2 step process (even if its still all on 1 page)
So once they make all selections, enable the file upload control so they can select a file. If at that point they go back and change something that forces a postback, they need to select the file again.
You can obviously do this however it meets the needs of the specific app you are doing, but something along those lines will probably be needed.
Re: File upload filename still getting cleared!
It's a partial postback, and yes, it's being cleared because the file browse button is inside the UpdatePanel. You will need to remove it, and you can do this by moving it out of the ContentTemplate for the UpdatePanel. Also keep in mind that the UpdatePanel doesn't work for File Upload controls (for uploading); only keep things in the UpdatePanel that need refreshing or can be affected by partial postbacks. There's no need to put everything in there.
1 Attachment(s)
Re: File upload filename still getting cleared!
Ok - here's a screen shot we just got from the client on how they want the page laid out.
Click MORE and an additional text box for e-mail appears (up to 4)
Hit the CALENDAR and a calendar control appears that will fill a date into the DEADLINE field.
Click STANDARD TURNAROUND and words "STANDARD TURNAROUND" appear in the DEADLINE field.
Click the GLOBE and a huge panel appears with all the languages or all the countries with check boxes - you can set all you want.
Now you BROWSE - and UPLOAD - grid below fills with rows from the database about the upload that just occured.
Where would you all use update panels and where would you use JS in all this?
Re: File upload filename still getting cleared!
I'd replace the calendar with a proper javascript calendar control so that no postback or partial postback is required.
If the globe does a partial postback, then I'd use an UpdatePanel just around it.
And around the MORE link as well, giving you a total of two updatepanels.
Re: File upload filename still getting cleared!
Re: File upload filename still getting cleared!
Quote:
Originally Posted by
wey97
Why not use Validators?
And what is a validator?
Re: File upload filename still getting cleared!
Validators are those controls in ASP.NET which do things like 'ensure field1 is filled out' or 'ensure field7 is a value greater than 35'.
Re: File upload filename still getting cleared!
Autopostback checkboxes (or anything autopostback) even on an update panel aren't a good idea IMO. If you use validators, you won't need to post back to see the check.
Unfortunately, there is no validator to force a checkbox check but it's simple to write one using a custom validator. There are plenty of examples if you search.
Just a word of warning, but update panels aren't the magic they're made out to be.
http://msdn.microsoft.com/en-us/magazine/cc163413.aspx