Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
http://csharptuning.blogspot.com/2007/05/uploading-multiple-files.html

I'm trying to build a webform that can upload any number of files and send as email attachment. I use the guide linked above. Howvever, when i try to build the code I get "Using the generic type 'System.Collections.Generic.List<T>' requires '1' type arguments" I'm not sure how to fix this. The guide wants this attribute to save a list of fileupload controls to Session["AddedControls"]
protected List AddedControls
{
get
{
if (Session["AddedControls"] == null)
Session["AddedControls"] = new List();
return (List)Session["AddedControls"];
}
set
{
Session["AddedControls"] = value;
}
}



full code
file_upload2.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class file_upload2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
        
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
}
    
    protected void Page_PreInit(object sender, EventArgs e)
{
foreach (Control ctrl in AddedControls)
{
divFileUpload.Controls.Add(ctrl);
}
}

protected void btnUpload_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile file = (HttpPostedFile)Request.Files[i];
if (file.ContentLength > 0)
{
try
{
file.SaveAs(Server.MapPath("../") + "\\tmp\\" + file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1));
}
catch (Exception ex)
{
Response.Write("" + ex.Message + "");
continue;
}
}
}
}
 
protected List AddedControls
{
get
{
if (Session["AddedControls"] == null)
Session["AddedControls"] = new List();
return (List)Session["AddedControls"];
}
set
{
Session["AddedControls"] = value;
}
}

    protected void lnkAddMore_Click(object sender, EventArgs e)
{
FileUpload fileUpload = new FileUpload();
Literal lt = new Literal();
lt.Text = "<br/>";
divFileUpload.Controls.Add(fileUpload);
divFileUpload.Controls.Add(lt);
AddedControls.Add(fileUpload);
AddedControls.Add(lt);
}

}


file_upload2.aspx
<%@ Page Language="C#"  AutoEventWireup="true" CodeFile="file_upload2.aspx.cs" Inherits="file_upload2" Title="Fileupload" %>
                                                            
<div>FileName: 
<asp:FileUpload ID="FileUpload1" runat="server" /> 
<asp:LinkButton ID="lnkAddMore" Text='Add More...' runat="server" OnClick="lnkAddMore_Click" /></asp:LinkButton> 
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /><br /> 
<div runat="server" id="divFileUpload" /> </div></div>
Posted

1 solution

look sir;
class List is a templated class once you use it you should provide a type argument like:

C#
protected List<byte> AddedControls
{
  get
  {
    if (Session["AddedControls"] == null)
    Session["AddedControls"] = new List<byte>();
    return (List<byte>)Session["AddedControls"];
  }
  set
  {
    Session["AddedControls"] = value;
  }
}
 
Share this answer
 
v3
Comments
techstress 27-May-11 19:14pm    
now i get this ASP Error

Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted.
Ali Al Omairi(Abu AlHassan) 28-May-11 7:46am    
I am sorry about the first one.
look you get this error because you are trying to serialize an array of type FileUpload which is non-serializable. i think you should use fileupload.FileBytes to save in the session. look at my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900