简体   繁体   中英

Gridview File Upload Control loses files on add more rows ASP.Net,C#

I am having Gridivew with fileupload which has Add more rows operation. When i Click on Add more rows the fileupload control loses the file. I need to save all the added rows on a single submit button which should save all the files on a folder. Kindly help me.

Thanks in advance

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

  1. Its extremely complicated.
  2. Bad UX, it confuses the user. He wont know if there really is a file.
  3. Clutters the ViewState immensely.

So these are the things you can do,

  1. Prompt the user that files will be lost when they click to add the files by asking their confirmation.
  2. Put a textbox and button asking user to pre-enter the number of files they want to add.
  3. Use a third party plugin that use ajax to add rows.(jqGrid and datatables.net are good.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM