Forum Discussion

Jerry8989's avatar
Jerry8989
Copper Contributor
Feb 21, 2024

How to edit fileupload control in Gridview without clearing all other fileupload controls?

I'm tasked with creating a asp.net website that will allow a user to submit names and pdf documents for people on their project.  For each person there needs to be 4 fileupload controls.  I was trying to do this with a gridview, but everytime I edit a row and update all the fileupload controls clear out.

 

I'm using C#, .net 4.7.2

 

1 Reply

  • RamyaDasari83's avatar
    RamyaDasari83
    Copper Contributor

     

    Here's an example:

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.IO" %>
    <script type="text/c#" runat="server">    protected void BtnUpload_Click(object sender, EventArgs e)
        {
            if (Request.Files != null)
            {
                foreach (string file in Request.Files)
                {
                    var uploadedFile = Request.Files[file];
                    if (uploadedFile.ContentLength > 0)
                    {
                        var appData = Server.MapPath("~/app_data");
                        var fileName = Path.GetFileName(uploadedFile.FileName);
                        uploadedFile.SaveAs(Path.Combine(appData, fileName));
                    }
                }
            }
        }</script>
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <form id="Form1" runat="server" enctype="multipart/form-data">
            <a href="#" id="add">Add file</a>
            <div id="files"></div>
            <asp:LinkButton ID="BtnUpload" runat="server" Text="Upload" OnClick="BtnUpload_Click" />
        </form>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
        <script type="text/javascript">        $('#add').click(function () {
                $('#files').append($('<input/>', {
                    type: 'file',
                    name: 'file' + new Date().getTime()
                }));
                return false;
            });
        </script>
    </body>
    </html>