Upload multiple files in ASP.Net GridView row editing using C# and VB.Net

kankon
 
on Jun 27, 2022 10:56 PM
Sample_135503.zip
416 Views

ASP.Net multi File Upload in GridView 

Is there chance to add multi upload, so if i have 5 pdf files upload them in 1 time?

<asp:TemplateField HeaderText="مرفقات">
    <itemtemplate>
        <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile"
            CommandArgument='<%# Eval("Id") %>' Visible='<%# !string.IsNullOrEmpty(Eval("Namefile").ToString()) ? true : false %>'>
        </asp:LinkButton>
    </itemtemplate>
    <edititemtemplate>
        <asp:FileUpload ID="fuFile" runat="server"/>
    </edititemtemplate>
    <controlstyle width="100px" />
    <itemstyle horizontalalign="Center" width="70px" />
</asp:TemplateField>
<asp:TemplateField>
    <edititemtemplate>
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Namefile") %>'></asp:TextBox>
    </edititemtemplate>
    <itemtemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Namefile") %>' Font-Size="1"></asp:Label>
    </itemtemplate>
</asp:TemplateField>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 28, 2022 05:45 AM
on Jul 01, 2022 04:57 AM

Hi kankon,

In order to enable multiple files selection in ASP.Net FileUpload, you need to set the AllowMultiple property to true.

When the form is submitted, a loop will be executed over the HttpPostedFile collection and one by one each file will be saved.

For more details refer below article.

Upload multiple files with ASP.Net 4.5 FileUpload control in Visual Studio 2012 and 2013

Refer below example.

HTML

C#

<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowEditing="OnRowEditing">
    <Columns>
        <asp:TemplateField HeaderText="Download">
            <ItemTemplate>
                <asp:LinkButton ID="lnkDownload" runat="server" Text="Download"
                    CommandArgument='<%# Eval("Id") %>' Visible='<%# !string.IsNullOrEmpty(Eval("Name").ToString()) ? true : false %>'>
                </asp:LinkButton>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:FileUpload ID="fuFiles" runat="server" AllowMultiple="true" />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" />
                <asp:LinkButton Text="Cancel" runat="server" OnClick="OnCancel" />
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

VB.Net

<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowEditing="OnRowEditing">
    <Columns>
        <asp:TemplateField HeaderText="Download">
            <ItemTemplate>
                <asp:LinkButton ID="lnkDownload" runat="server" Text="Download"
                    CommandArgument='<%# Eval("Id") %>' Visible='<%# Not If(String.IsNullOrEmpty(Eval("Name").ToString()), True, False) %>'>
                </asp:LinkButton>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:FileUpload ID="fuFiles" runat="server" AllowMultiple="true" />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" />
                <asp:LinkButton Text="Cancel" runat="server" OnClick="OnCancel" />
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;
using System.IO;

VB.Net

Imports System.Data
Imports System.IO

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGridView();
    }
}

protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
    gvCustomers.EditIndex = e.NewEditIndex;
    this.BindGridView();
}

protected void OnUpdate(object sender, EventArgs e)
{
    GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
    string id = (row.Cells[1].Controls[0] as TextBox).Text;
    string name = (row.Cells[2].Controls[0] as TextBox).Text;
    string folderPath = Server.MapPath("~/Files/");
    if (!Directory.Exists(folderPath))
    {
        Directory.CreateDirectory(folderPath);
    }

    foreach (HttpPostedFile postedFile in (row.FindControl("fuFiles") as FileUpload).PostedFiles)
    {
        if (postedFile.ContentLength > 0)
        {
            string fileName = Path.GetFileName(postedFile.FileName);
            postedFile.SaveAs(folderPath + fileName);
        }
    }
    gvCustomers.EditIndex = -1;
    this.BindGridView();
}

protected void OnCancel(object sender, EventArgs e)
{
    gvCustomers.EditIndex = -1;
    this.BindGridView();
}

private void BindGridView()
{
    using (DataTable dt = new DataTable())
    {
        dt.Columns.AddRange(new DataColumn[] {
                            new DataColumn("Id"),
                            new DataColumn("Name") });
        dt.Rows.Add(1, "John Hammond");
        dt.Rows.Add(2, "Mudassar Khan");
        dt.Rows.Add(3, "Suzanne Mathews");
        dt.Rows.Add(4, "Robert Schidner");
        gvCustomers.DataSource = dt;
        gvCustomers.DataBind();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindGridView()
    End If
End Sub

Protected Sub OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    gvCustomers.EditIndex = e.NewEditIndex
    Me.BindGridView()
End Sub

Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
    Dim row As GridViewRow = TryCast((TryCast(sender, LinkButton)).NamingContainer, GridViewRow)
    Dim id As String = (TryCast(row.Cells(1).Controls(0), TextBox)).Text
    Dim name As String = (TryCast(row.Cells(2).Controls(0), TextBox)).Text
    Dim folderPath As String = Server.MapPath("~/Files/")

    If Not Directory.Exists(folderPath) Then
        Directory.CreateDirectory(folderPath)
    End If

    For Each postedFile As HttpPostedFile In TryCast(row.FindControl("fuFiles"), FileUpload).PostedFiles
        If postedFile.ContentLength > 0 Then
            Dim fileName As String = Path.GetFileName(postedFile.FileName)
            postedFile.SaveAs(folderPath & fileName)
        End If
    Next

    gvCustomers.EditIndex = -1
    Me.BindGridView()
End Sub

Protected Sub OnCancel(ByVal sender As Object, ByVal e As EventArgs)
    gvCustomers.EditIndex = -1
    Me.BindGridView()
End Sub

Private Sub BindGridView()
    Using dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn() {
                            New DataColumn("Id"),
                            New DataColumn("Name")})
        dt.Rows.Add(1, "John Hammond")
        dt.Rows.Add(2, "Mudassar Khan")
        dt.Rows.Add(3, "Suzanne Mathews")
        dt.Rows.Add(4, "Robert Schidner")
        gvCustomers.DataSource = dt
        gvCustomers.DataBind()
    End Using
End Sub

Screenshot