Mr. Smith
Mr. Smith

Reputation: 5558

Iterating Dynamic FileUpload Control Collection in Panel Control using ASP.NET C#

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?

Upvotes: 3

Views: 4637

Answers (1)

Harrison
Harrison

Reputation: 9090

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

Upvotes: 1

Related Questions