使用 ASP.NET C# 迭代面板控件中的动态 FileUpload 控件集合
我正在尝试获取添加到面板中的动态生成的 FileUpload 控件的值:
<asp:Panel ID="pFileControls" runat="server">
</asp:Panel>
我在循环记录集期间创建控件:
foreach(DataRow dr in ds.Tables[0].Rows)
{
FileUpload fu = new FileUpload();
fu.ID = dr["SomeID"].ToString();
pFileControls.Controls.Add(fu);
}
正常,直到我使用此按钮提交表单:
<asp:Button ID="btnImportFile" runat="server" Text="Save" OnClick="btnImportFile_Click" />
一切 像这样注册(Page_Load):
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnImportFile);
我这样做是因为我在网站中使用 MasterPage/ContentPage 设置,并且大多数情况都发生在 UpdatePanel 内以实现 AJAXification 目的。请记住,如果我在 HTML 视图中明确指定 FileUpload 控件,则它可以 100% 工作。
提交表单后,我尝试像这样迭代面板:
foreach (Control ctrl in pFileControls.Controls)
{
if (ctrl.GetType() != typeof(FileUpload))
{
continue;
}
//Do the saving of the file here
}
除此之外,面板似乎只返回一个控件:页面的内容占位符,仅此而已。有人对此有一些想法吗?
I'm trying to get the values of dynamically generated FileUpload controls that I add to a Panel:
<asp:Panel ID="pFileControls" runat="server">
</asp:Panel>
I create the controls during a loop through a record set:
foreach(DataRow dr in ds.Tables[0].Rows)
{
FileUpload fu = new FileUpload();
fu.ID = dr["SomeID"].ToString();
pFileControls.Controls.Add(fu);
}
Everything works fine up to the point where I submit the form with this button:
<asp:Button ID="btnImportFile" runat="server" Text="Save" OnClick="btnImportFile_Click" />
Which I register like this (Page_Load):
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnImportFile);
I do this because I'm using a MasterPage/ContentPage setting in my website and mostly everything happens inside an UpdatePanel for AJAXification purposes. Bear in mind that if I explicity specify a FileUpload Control in the HTML view, it works 100%.
When the form is submitted I try to iterate the Panel like this:
foreach (Control ctrl in pFileControls.Controls)
{
if (ctrl.GetType() != typeof(FileUpload))
{
continue;
}
//Do the saving of the file here
}
Except, the Panel seems to only return one control: The Content Place Holder for the page and nothing else. Does anyone have some ideas about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要在生命周期的哪一部分添加动态控件?
如果将它们放入 page_load 中,可能为时已晚,请尝试将动态控件的生成放入 page_init 中,看看是否可以解决问题。
页面生命周期
http://msdn.microsoft.com/en-us/library/ms178472.aspx
动态控件
http://geekswithblogs.net/shahed/archive/2008/06/26 /123391.aspx
笔记:
我希望即使使用更新面板,您也需要注意动态控件的 page_load 限制。
让我知道这是否有帮助或者我是否错过了标记!
让我们尝试不同的操作过程(我已经得到了动态文件上传工作,但它是一个熊,我希望我只是使用这个)
http://www.asp.net/ajaxlibrary/act_AsyncFileUpload.ashx
或
http://en.fileuploadajax.subgurim.net/
这些可能不会创建元素的“循环” ,但您可以简单地根据需要继续加载文档。
我专门用过
http://www.asp.net/ajaxlibrary/act_AsyncFileUpload.ashx
达到很好的效果。
更新:面板和文件上传似乎也有一些限制,请查看这些网站。
(这个说它在部分更新状态下不起作用,但在完整回发状态下起作用)
http://forums.asp.net/p/1105208/1689084.aspx
您知道提交是触发整个页面还是仅触发更新:面板? (看看这个:http://geekswithblogs.net/ mmintoff/archive/2009/04/01/fileupload-within-updatepanel.aspx
What part of the life cycle are you adding the dynamic controls?
if you are putting them in the page_load it may be too late, try putting the generation of the dynamic controls into the page_init and see if that fixes the problem.
page lifecycle
http://msdn.microsoft.com/en-us/library/ms178472.aspx
dynamic controls
http://geekswithblogs.net/shahed/archive/2008/06/26/123391.aspx
Note:
I would expect that even with the update panel, you will need to be mindful of the page_load limitations with dynamic controls.
let me know if this helps or if I missed the mark!
Let's try a different course of action (I've gotten dynamic file upload to work, but it was a bear and I wish I had simply used this)
http://www.asp.net/ajaxlibrary/act_AsyncFileUpload.ashx
or
http://en.fileuploadajax.subgurim.net/
these may not create a 'loop' of elements, but you can simply keep loading docs on a as-needed basis.
I have specifically used
http://www.asp.net/ajaxlibrary/act_AsyncFileUpload.ashx
to great effect.
There also appear to be some limitations to the update:panel and the file upload, check out these sites.
(this one says it does not work in partial update status but does work in full postback)
http://forums.asp.net/p/1105208/1689084.aspx
do you know if the submit is triggering the full page or just the update:panel? (check out this: http://geekswithblogs.net/mmintoff/archive/2009/04/01/fileupload-within-updatepanel.aspx