Reputation: 345
I have problem with "manualy" setting file to FileLoad.
So here's my situation:
I'm using this manual: http://www.codeproject.com/Tips/101834/How-to-Maintain-FileUpload-Control-s-State-after-P?msg=4176652#xx4176652xx
I've need to remember loaded file after PostBack on page. So I have FileLoad object and Button which will do PostBack. After post back I set Session["MenuFile"] = FileLoad;, session record is created but, when I try to set this file back to FileLoad object it realy get there(I can see the file on LoadFile object). But on page it's empty. I've tried to set it even on Load, Init events on FileLoad object and nothing works.
It's like FileLoad successfully loaded file from session and than "reseted" to default settings(blank).
And heres my code:
<tr>
<td>
<asp:Label runat="server" Text="Menu:"></asp:Label></td>
<td>
<asp:FileUpload runat="server" ID="fuMenu"/>
<asp:RequiredFieldValidator runat="server" ID="rfvMenu" ControlToValidate="fuMenu" ErrorMessage="Menu file is required" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button runat="server" ID="neco" />
<asp:ValidationSummary runat="server"/>
</td>
</tr>
And code behind:
protected void Page_Load(object sender, EventArgs e)
{
// If first time page is submitted and we have file in FileUpload control but not in session
// Store the values to Session Object
if (Session["MenuFile"] == null && fuMenu.HasFile)
{
Session["MenuFile"] = fuMenu;
}
// Next time submit and Session has values but FileUpload is Blank
// Return the values from session to FileUpload
else if (Session["MenuFile"] != null && (!fuMenu.HasFile))
{
fuMenu = (FileUpload)Session["MenuFile"];
}
// Now there could be another sictution when Session has File but user want to change the file
// In this case we have to change the file in session object
else if (fuMenu.HasFile)
{
Session["MenuFile"] = fuMenu;
}
}
I've need to work with session so I can't use something like this: How to Maintain FileUpload Control’s State after PostBack Information disappears on button click
I'm realy desperate and I'll be greatfull for any kind of help. Thanks !
Upvotes: 1
Views: 238
Reputation: 28
The ASP:FileUpload is read-only by design for security reasons. You cannot set anything into it after postback.
I'm using a label instead of the fileupload after postback as a feedback to the user that there was a file uploaded.
My postback checks the fileupload for content, uploads the file, hides the fileupload, shows the label containing the filename (stored in Session Object) and also shows a delete button which hides the label, removes the session object, deletes the file and shows the fileupload again after another postback.
Upvotes: 1