Click here to Skip to main content
15,918,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please Helpm

When i click btnUpload its says "file upload Has No File".

ASP.NET
<asp:GridView ID="GridViewUploadedFile" runat="server" class="table table-striped table-bordered table-hover" AutoGenerateColumns="False" EmptyDataText="No files found!" Caption="Property Under Details" DataKeyNames="DocData_ID" DataSourceID="SDSPU" OnSelectedIndexChanged="GridViewUploadedFile_SelectedIndexChanged">
                                    <Columns>
                                        <asp:CommandField ShowEditButton="True" />
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:LinkButton runat="server" ID="lbluploaddoc" Text="UploadDoc" CommandName="Select"></asp:LinkButton>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="DocData_ID" HeaderText="" InsertVisible="False" ReadOnly="True" SortExpression="DocData_ID" />
                                        <asp:BoundField DataField="PU_Name" HeaderText="" ControlStyle-Width="80px" SortExpression="PU_Name" />
                                        <asp:BoundField DataField="PUC_Name" HeaderText="" ControlStyle-Width="80px" SortExpression="PUC_Name" />
                                        <asp:BoundField DataField="Doc_Required" HeaderText="Required" ControlStyle-Width="221px" SortExpression="Doc_Required" />
                                        <%--<asp:BoundField DataField="Doc_Date" HeaderText="Date" ControlStyle-Width="80px" SortExpression="Doc_Date" NullDisplayText="01/01/2099" DataFormatString="{0:dd/MM/yyyy}" ControlStyle-CssClass="m-wrap m-ctrl-medium date-picker" />--%>
                                        <asp:TemplateField HeaderText="Date">
                                            <EditItemTemplate>
                                                <asp:TextBox ID="lbldocdate" Width="80px" class="m-wrap m-ctrl-medium date-picker" runat="server" Text='<%# Bind("Doc_Date","{0:dd/MM/yyyy}") %>'></asp:TextBox>
                                            </EditItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="Remarks" HeaderText="Remarks" SortExpression="Remarks" />
                                        <asp:TemplateField HeaderText="List of Files" HeaderStyle-Width="50%">
                                            <ItemTemplate>
                                                <asp:HyperLink ID="HyperLink1" Target="_blank" runat="server" Text='<%# Eval("Doc_File_Name") %>'
                                                    NavigateUrl='<%# Eval("File_Path") %>'>
                                                </asp:HyperLink>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="File Upload" HeaderStyle-Width="90%">
                                            <ItemTemplate>
                                                <%--<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                                                    <ContentTemplate>
                                                                                                              
                                                    </ContentTemplate>                                                                                                                                               
                                                    </asp:UpdatePanel>     --%>
                                                <asp:FileUpload ID="FUDocs" runat="server"/>                                             
                                                <asp:linkbutton ID="btnUpload" runat="server" class="btn blue" Text="Upload Document File" OnClick="btnUpload_Click"></asp:linkbutton>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>


C#
private void UploadDoc()
    {        
            //Get path from web.config file to upload        
            bool blSucces = false;
            //To check whether file is selected or not to uplaod
            FUDocs = (FileUpload)GridViewUploadedFile.FindControl("FUDocs");
            btnUpload = (Button)GridViewUploadedFile.FindControl("btnUpload");
            if (FUDocs.HasFile)
            {
                try
                {
                    string[] allowdFile = { ".pdf" };
                    //Here we are allowing only pdf file so verifying selected file pdf or not
                    string FileExt = System.IO.Path.GetExtension(FUDocs.PostedFile.FileName);
                    bool isValidFile = allowdFile.Contains(FileExt);
                    if (!isValidFile)
                    {
                        lblmsg.ForeColor = System.Drawing.Color.Red;
                        lblmsg.Text = "Please upload only pdf ";
                    }
                    else
                    {
                        // Get size of uploaded file, here restricting size of file
                        int FileSize = FUDocs.PostedFile.ContentLength;
                        if (FileSize <= 1000000)//1048576 byte = 1MB
                        {
                            //Get file name of selected file
                            filename = lblpsrno.Text + lblpuc.Text + FileExt;
                            //Save selected file into specified location
                            FUDocs.SaveAs(Server.MapPath(FilePath) + filename);
                            lblmsg.Text = "File upload successfully!";
                            blSucces = true;
                        }
                        else
                        {
                            lblmsg.Text = "Attachment file size should not be greater then 1 MB!";
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblmsg.Text = "Error occurred while uploading a file: " + ex.Message;
                }
            }
            else
            {
                lblmsg.Text = "Please select a file to upload.";
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Please select a file to upload.');", true);
            }
            if (blSucces == true)
            {
                Updatefileinfo();
            }
        
    }
Posted

1 solution

Add following code in aspx page under gridview:
ASP.NET
<asp:templatefield>
	<itemtemplate>
		<asp:fileupload id="FUDocs" runat="server" />                                             
		<asp:linkbutton id="btnUpload" runat="server" class="btn blue" text="Upload Document File" onclick="btnUpload_Click">
		</asp:linkbutton> 
	</itemtemplate>
</asp:templatefield>

Add following code in code-behind file:
C#
protected void btnUpload_Click(object sender, GridViewUpdateEventArgs e)   
{
	int index = e.RowIndex;
	GridViewRow row = (GridViewRow)gv1.Rows[index];  

	FileUpload fu = (FileUpload) row.FindControl("FUDocs");  

	if (fu.HasFile)  
	{
		string file = System.IO.Path.Combine(Server.MapPath("~/Images/"), fu.FileName);  
		fu.SaveAs(file);
		
		// Implement your logic
	}		
}
 
Share this answer
 
v3
Comments
Member 11014751 25-Jan-16 0:11am    
Error : Error 24 No overload for 'btnUpload_Click' matches delegate 'System.EventHandler'
Member 11014751 25-Jan-16 0:53am    
Please Help..
[no name] 25-Jan-16 1:23am    
Try like this:

protected void btnUpload_Click(object sender, EventArgs e)
{
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;

FileUpload fu = (FileUpload)grdrow.FindControl("FUDocs");

if (fu.HasFile)
{
string file = System.IO.Path.Combine(Server.MapPath("~/Images/"), fu.FileName);
fu.SaveAs(file);

// Implement your logic
}
}
Member 11014751 25-Jan-16 2:17am    
Now it is saying FileUpload Has No File.....
[no name] 25-Jan-16 2:33am    
Not sure what is going on with you.Follow below link and implement as per that.

http://www.codeproject.com/Questions/228074/how-to-manage-file-upload-control-inside-grid-view

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900