gpt4 book ai didi

c# - FileUpload 上传代码隐藏。

转载 作者:行者123 更新时间:2023-11-30 16:22:19 28 4
gpt4 key购买 nike

如何仅使用 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);
}
}

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

         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 似乎无法做到。

最佳答案

在下面的点击事件中,您得到的是文本框而不是文件上传控件

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

关于c# - FileUpload 上传代码隐藏。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12513949/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com