I have an ASP.NET Repeater control with a FileUpload Control in its item template. The ASP.NET Repeater is inside an UpdatePanel as part of a wizard made from an ASP.NET Multiview. I am trying to upload every file in each FileUpload control when I click submit, but the FileUpload controls do not retain a file when I submit the form. Here is the relevant code:
<asp:Repeater ID="RepeaterImages" runat="server" OnItemCommand="RepeaterBoxArts_ItemCommand">
<ItemTemplate>
<tr>
<td class="right">
Choose File:
</td>
<td>
<asp:FileUpload ID="FileUpload" runat="server" />
</td>
</tr>
</ItemTemplate>
foreach (RepeaterItem item in RepeaterImages.Items)
{
if (item.ItemType == ListItemType.Item
|| item.ItemType == ListItemType.AlternatingItem)
{
FileUpload fupload = (FileUpload)item.FindControl("FileUpload");
if (fupload.HasFile)
{
string path = Server.MapPath("~/images/");
fupload.SaveAs(path);
}
}
}
When I click Submit, I want all FileUpload controls with a file to upload their files. However, if I run the step debugger, it shows each FileUpload control as not having a file. It seems to be an issue with postbacks, but I'm not 100% sure.
Best Answer
I had the same problem and solved adding this code
OnClientClick="javascript:document.forms[0].encoding = 'multipart/form-data';"
at the button that will upload all images.
The answer of this problem, I found at this link https://stackoverflow.com/a/217722
See my code below:
In the page ASPX
In the CodeBehind