3

如何仅使用 FileUpload 代码隐藏上传?我的控件是代码隐藏的,因为我有 Dropdown_SelectedIndexChanged 并且需要生成各种数量的控件。我可以很好地列出控件并将文件和文本附加到txtbox:

        private void SetChildrenCountControls(int total)
        {
          for (int i = 0; i < total; i++)
          {
            var tbBirthDate = new TextBox();
            tbBirthDate.ID = "tbBirthDate_" + (i + 1);
            tbBirthDate.CssClass = "tbSister_input";
            tbBirthDate.EnableViewState = true;

            FileUpload upload = new FileUpload();
            upload.ID = "imgUpload_" + (i + 1);
            upload.CssClass = "tbSister_upload";
            upload.EnableViewState = true;

            ChildrenCountTextPanel.Controls.Add(tbBirthDate);
            ChildrenCountTextPanel.Controls.Add(upload);
          }
       }    

并且可以通过以下方式获取 txtbox 中输入的文本:

         protected void lbFamilySave_Click(object sender, EventArgs e)
    {
        var countSisters = ChildrenCountTextPanel.Controls.OfType<TextBox>();
        string sisterBirth = string.Empty;
        foreach (var sister in countSisters)
        {
            if (sister.ID.Contains("tbBirthDate_"))
                sisterBirth = sister.Text;
        }
    } 

如何从 FileUpload 控件获取文件?似乎无法使用 FileUpload 完成上述操作。

4

2 回答 2

3

在下面的单击事件中,您将获得 TextBoxes 而不是 FileUpload Control

btn_protected void lbFamilySave_Click(object sender, EventArgs e)
{
        var countSisters = ChildrenCountTextPanel.Controls.OfType<TextBox>();

文件上传控件属于这种类型 System.Web.UI.WebControls.FileUpload

因此,请获取 FileUpload 控件,然后执行以下操作:

if (myFileUpload.HasFile)
{
  string savePath = @"C:\Temp\" + myFileUpload.FileName;

  myFileUpload.SaveAs(savePath);
}
于 2012-09-20T13:57:51.600 回答
0

得到它的工作,以为我已经尝试过。显然不够好:)

 var countUploads = ChildrenCountTextPanel.Controls.OfType<FileUpload>();

        FileUpload FileUl = new FileUpload();
        foreach (var ul in countUploads)
        {
            if(ul.ID.Contains("imgUpload_"))
            {
                FileUl = ul;               
            }
        }
if (FileUl.HasFile)
{
 ConvertAndSave(FileUl)
 }
于 2012-09-20T17:00:08.663 回答