1

How can I upload a file through a dynamically generated file upload control and a click on the dynamically generated button's event?

Following is the code I have tried, but it's not working:

void UpLdButton_Click(object sender, EventArgs e)
{
    Button btnUpLD = sender as Button;
    btnUpLD = (Button)this.FindControl(Form.ID).FindControl(table.ID).FindControl(row.ID).FindControl(cell.ID).FindControl(UpLdButton.ID);

    fileUp = (FileUpload)this.FindControl(Form.ID).FindControl(table.ID).FindControl(row.ID).FindControl(cell.ID).FindControl(fileUp.ID);
    string args = ((Button)sender).CommandArgument;
    //lblTest.Text = args;        
    //UploadFile(fileUp);

    if (fileUp.HasFile)
    {
        fileUp.PostedFile.SaveAs(Server.MapPath("~\\") + fileUp.FileName);            
        Label1.Text = "The" + fileUp.FileName + " Has been uploaded";
    }
    else
    {
        Label1.Text = "FILE not selected";
    }
}

Thanks in Advance!!!

dgvid
  • 26,293
  • 5
  • 40
  • 57
Nishant SB
  • 67
  • 2
  • 16
  • And what is not working? at least there is no upload because UploadFile is commented out. – RvdK Mar 07 '13 at 14:36
  • I am not using UploadFile(). Whenever I run this code control goes to the else part of condition even when I select file from Filuplpad control. – Nishant SB Mar 07 '13 at 14:43
  • So HasFile returns false, have you looked at http://stackoverflow.com/questions/13138247/fileupload-hasfile-give-always-false – RvdK Mar 07 '13 at 15:30
  • Might be a stupid comment, but have you checked that your Form is enctype="multipart/form-data" ? (never checked if it is automatically added for dynamically added controls) Also, do you add your controls during init ? – jbl Mar 07 '13 at 15:57

0 Answers0