使用 ASP.NET C# 迭代面板控件中的动态 FileUpload 控件集合

发布于 2024-09-27 18:57:23 字数 1035 浏览 2 评论 0原文

我正在尝试获取添加到面板中的动态生成的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你好,陌生人 2024-10-04 18:57:23

您要在生命周期的哪一部分添加动态控件?

如果将它们放入 page_load 中,可能为时已晚,请尝试将动态控件的生成放入 page_init 中,看看是否可以解决问题。

页面生命周期
http://msdn.microsoft.com/en-us/library/ms178472.aspx

动态控件
http://geekswithblogs.net/shahed/archive/2008/06/26 /123391.aspx
笔记:

“建议加载动态
相反,在 Page_Init 期间进行控制,
因为我们可能想连接我们的
尽早有适当的处理程序的事件
阶段。 ...不要分配
动态控件的属性
(启用视图状态),在 Page_Init 期间,
它不会被反映。 ”

我希望即使使用更新面板,您也需要注意动态控件的 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:

"Its recommended to load the dynamic
controls during the Page_Init instead,
because we may want to hook up our
events with proper handler at an early
stage. ... Do not assigning
properties of a dynamic control
(viewstate enabled), during Page_Init,
it will not be reflected. "

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文