You cannot persist the input type="file"
state after an HTTPPost. ( In ASP.NET jargon this will translate as cannot persist asp:FileUpload
s value after PostBack ).
Its designed that way to address security issues. There are so many examples of the same question answered here at StackOverflow ( search it ). There is a Session
hack that provides a workaround but its for single file. Link here: https://stackoverflow.com/a/18656681/17447
Theoretically you could still do it using a Dictionary<int, FileUpload>
. The first parameter will the GridViewRow.RowIndex
and the second parameter would be the asp:FileUpload
control on that row. You can start by adding all rows at the first PostBack to a session and then updating it when file.HasFile and so. But IMHO, don't do that because
- Its extremely complicated.
- Bad UX, it confuses the user. He wont know if there really is a file.
- Clutters the ViewState immensely.
So these are the things you can do,
- Prompt the user that files will be lost when they click to add the files by asking their confirmation.
- Put a textbox and button asking user to pre-enter the number of files they want to add.
- Use a third party plugin that use ajax to add rows.(jqGrid and datatables.net are good.)