views:

50

answers:

1

Hello everyone,

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?

+1  A: 

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

tanging
Thank you very much, the second link you gave me cleared it up. I just had to load the controls inside Page_Load instead!
Mr. Smith
Ok, maybe I was a bit too hasty. Because I'm recreating the controls everytime on Page_Load, whatever values they contained when they were submitted are lost. Any ideas?
Mr. Smith
@mr. Smith, you will need to create the controls on each page load. place this part into the page_init, they will be recreated on each load but when the viewstate gets loaded your values will be re-inputted (via the viewstate). by page_load the viewstate is already loaded
tanging
Thanks for replying. I tried putting the dynamic generation in Page_Init with no success. When I get to the iteratation part it does find the controls, but they have no "file" (i.e FileUpload.HasFile).
Mr. Smith
@Mr. Smith, getting the file loader in ajax can be a trying experience, I would put forth trying out the asp.net ajax toolkit (which I have gotten to work with the file upload) or any of the other free ones out there (I linked to one that looked quite nice). As for the no file, I would still wonder the lifecycle stage and when you create the control you aren't putting in any properties that could overwrite the postback data?
tanging
I don't set anything other than those two lines of code: FileUpload fu = new FileUpload(); and fu.ID = dr["SomeID"].ToString();. This now happens in Page_Init. I'm still trying a couple of things though, will update soon.
Mr. Smith
Ok, this is very strange. When I submit (click the upload button a second time), it does the upload (the controls have files). Finding it hard to understand this behaviour. It should work the first time around?
Mr. Smith
@Mr. Smith ahh the joys of dynamic controls. It sounds like the event is not getting wired up on the initial page load and/or the panel load. Are there any items that are occurring in a isPostBack clause that may be causing the addition of the event? I am going to add some links above that may (hopefully!) set you on the correct course
tanging
Ok, I figured it out! Basically one of the buttons involved in the process was not registered as a post-back trigger. It's sorted now, thanks!
Mr. Smith
@Mr. Smith, could you post your code behind? (or to be brief specifically your page_load, page_init and your post back handling?)
tanging
@Mr. Smith glad to hear! happy programming!
tanging