开发者

ASP.NET Multiple Fileupload

I'm building a website (umbraco based) where the users are able to upload multiple images to their posts. What I have so far is:

<asp:TextBox MaxLength="1" Width="29px" runat="server" ID="txtImageAmount" />&nbsp;
<asp:UpdatePanel ID="UpdatePanel1" runat="serve开发者_开发知识库r">
    <ContentTemplate>
       <asp:Button ID="btnSubmitImageAmount" runat="server" Text="Vis upload felter" 
            onclick="btnSubmitImageAmount_Click" />

       <asp:Label Visible="false" ID="lblImageAmountError" ForeColor="Red" runat="server" Text="Maks 3 billeder"></asp:Label>  
       <asp:Panel ID="pnlUploadControls" Visible="false" runat="server"></asp:Panel>
    </ContentTemplate>
</asp:UpdatePanel>  

So the user is able to choose the amount of fileupload boxes (max 3 though) on the site.

My C# looks like this:

protected void btnSubmitImageAmount_Click(object sender, EventArgs e)
{
    int amountOfControls = Convert.ToInt32(txtImageAmount.Text);
    if (amountOfControls <= 3)
    {
        for(int i = 0; i < amountOfControls; i++)
        {
            FileUpload fUl = new FileUpload();
            fUl.ID = i.ToString();
            fUl.Width = 300;
            Label lblLinebreak = new Label();
            lblLinebreak.Text = "<br />";
            pnlUploadControls.Controls.Add(fUl);
            pnlUploadControls.Controls.Add(lblLinebreak);
            pnlUploadControls.Visible = true;
        }
    }
    else
    {
        lblImageAmountError.Visible = true;
    }
}

So basically I'm adding a new FileUpload control to the Panel depending on how many the user wants.

Now, in my Save button I have the following code:

List<Media> images = new List<Media>();

    foreach (FileUpload fUl in pnlUploadControls.Controls)
    {
        Media m = UmbracoSave(fUl);
        if (m != null)
        {
            images.Add(m);
        }       
    }

    if (images.Count > 0)
    {
        RelationType ad2media = RelationType.GetByAlias("ad2media");
        foreach (Media img in images)
        {
            Relation.MakeNew(adDoc.Id, img.Id, ad2media, adDoc.Text + " is related to " + img.Text);
        }
    }

I have tried to check if the amount of controls in the panel is equal to 0 and it seems it is.. The weird thing is, if I check if the Media item returned from the UmbracoSave method is null, it isn't.

Also, it says that the List (images) count is 0..

Can anyone shed some light on this? :-)

Any help is greatly appreciated!

All the best,

Bo


You can't put a standard FileUpload control in an UpdatePanel. It just doesn't work. Look at an AJAX compatible file upload component.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜