开发者

Adding FileUpload control via code-behind

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, "Inte开发者_C百科rnet Explorer cannot display the webpage".

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

Any ideas?

Thanks!!


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));
   }
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜