Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I need to select file from disk in each row of datagrid. I am using FileUpload as following but its not accessable
C#
fu = (FileUpload)row.FindControl("fu_doc");
on server side.

C#
<asp:TemplateField HeaderText="Select file">
 <itemtemplate>
   <asp:FileUpload ID="fu_doc"; runat="server"> 
 </itemtemplate>




Any suggestion?
Posted
Updated 14-Mar-12 21:20pm
v2

1 solution

try:
XML
  <asp:TemplateField HeaderText="Logo Upload">
    <ItemTemplate>
        <asp:FileUpload ID="FileUpload4" runat="server" /><asp:Button ID="bt_upload" runat="server" EnableViewState="False" Text="Upload" CommandName="Upload" />
</ItemTemplate>
</asp:TemplateField>

and in the serverside:
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
       Button bts = e.CommandSource as Button;
       Response.Write(bts.Parent.Parent.GetType().ToString());
       if (e.CommandName.ToLower() != "upload")
       {
           return;
       }
       FileUpload fu = bts.FindControl("FileUpload4") as FileUpload;//here
       if (fu.HasFile)
       {
           bool upload = true;
           string fleUpload = Path.GetExtension(fu.FileName.ToString());
           if (fleUpload.Trim().ToLower() == ".xls" | fleUpload.Trim().ToLower() == ".xlsx")
           {
               fu.SaveAs(Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString()));
               string uploadedFile = (Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString()));
               //Someting to do?...
           }
           else
           {
               upload = false;
               // Something to do?...
           }
           if (upload)
           {
               // somthing to do?...
           }
       }
       else
       {
           //Something to do?...
       }
   }


modify according to your requirement
 
Share this answer
 

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