1

Because of the environment I work in I need to add controls to a page via code-behind. I have done it dozens of times. For some reason the FileUpload control is giving me grief. here is my code:

FileUpload fileUpload = new FileUpload();
fileUpload.ID = "FileUploadControl";
this.Controls.Add(fileUpload);

The page looks as though it is timing out and display this error, "Internet Explorer cannot display the webpage".

When I remove the last line (the Add), then the page renders just fine.

Any ideas?

Thanks!!

1 Answer 1

0

You didn't mentioned which event handler you have used. Please try this,

 FileUpload file;
 protected void Page_Load(object sender, EventArgs e)
 {
  file= new FileUpload();
  PlaceHolder1.Controls.Add(file);
 }
 protected void Button1_Click(object sender, EventArgs e)
 {
  if(file.HasFile)
   {
    file.SaveAs(MapPath("~/" + file.FileName));
   }
 }
1
  • 1
    I added the event handler but found that the control is not exposing things like HasFile, FileName, SaveAs, or any of the FileUpload properties. Why would they be missing?
    – user390480
    Oct 3, 2011 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.