我想做一个多文件上传页面,后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace aspx深入
{
public partial class _Default : System.Web.UI.Page
{
int count=1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
createUF(1);//初始化添加一个控件
}
}
protected void Button1_Click(object sender, EventArgs e)//设置按钮
{
count= Convert.ToInt32(TextBox1.Text);
createUF(count);
}
//添加控件函数
protected void createUF(int count)
{
for (int i = 1; i <= count; i++)
{
FileUpload f = new FileUpload();
f.ID = "F" + i.ToString();
this.Form.Controls.Add(f);
}
}
protected void Button2_Click(object sender, EventArgs e)//上传按钮
{
try
{
string path = Server.MapPath("UP");
for (int i = 1; i <= count; i++)
{
FileUpload f = (FileUpload)(this.Form.FindControl("F" + i));
string fileName = f.FileName;
string saveName = path + fileName;
f.SaveAs(saveName);
Response.Write("<script>alert('上传成功!')</script>");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
但是报错了 :未将对象引用到实例。
我进行了调试,根本就无法通过ID访问这些动态创建的FileUpload控件,请问问题出在哪儿吗? |