asp.net调用动态控件的问题....郁闷啊....
public partial class add : System.Web.UI.Page
{
SqlConnection Conn = new SqlConnection("Data Source=JINLU;Initial Catalog=Socut;Integrated Security=SSPI ");
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)//这里动态生成上传图片的上传控件,数目有页面的下拉框指定
{
if (Int32.Parse(DropDownList2.Text) != 0)
{
for (int i = 0; i < Int32.Parse(DropDownList2.Text); i++)
{
FileUpload upLoad = new FileUpload();//动态生成了文件上传控件
upLoad.ID = "FileUpload" + i.ToString(); //动态编号
upLoad.Visible = true;
Panel1.Controls.Add(upLoad);//添加到panel
//---------添加图片...//
}
}
}
-----------------------之后我想在点击事件中执行数据上传操作!因为前面动态生成了文件上传控件
protected void btnAdd_Click(object sender, EventArgs e)
{
string strID = System.Guid.NewGuid().ToString();//GUID编号ID
if (Int32.Parse(DropDownList2.Text) > 0)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=JINLU;Database=Socut;Integrated Security=SSPI";
string sql = "Insert into image(id) values('" + strID + "')";
SqlCommand scmd = new SqlCommand(sql, conn);
conn.Open();
scmd.ExecuteNonQuery();
conn.Close();
for (int i = 0; i < Int32.Parse(DropDownList2.Text); i++)
{
HttpPostedFile upPhoto = ((FileUpload)UpdatePanel1.FindControl("FileUpload" + i.ToString())).PostedFile;//这里我引用刚才进行动态编号的ID值,想找到这个控件,结果没有找到。
int upPhotoLength = upPhoto.ContentLength;
byte[] PhotoArray = new Byte[upPhotoLength];
Stream PhotoStream = upPhoto.InputStream;
PhotoStream.Read(PhotoArray, 0, upPhotoLength);
//连接数据库
string strSql = " UPDATE image SET n_image" + i.ToString() + " =@FImage WHERE n_id='" + strID + "'";
scmd = new SqlCommand(strSql, conn);
scmd.Parameters.Add("@FImage", SqlDbType.Image);
scmd.Parameters["@FImage"].Value = PhotoArray;
conn.Open();
scmd.ExecuteNonQuery();
conn.Close();
Label1.Text = "正在添加第" + (i + 1).ToString() + "图片";
}
}
}
}
我的问题是怎么样才能调用到我动态生成的控件,因为那样我才能对其做一些操作啊。。findconctrol怎么会提示对象没有被引用。。。郁闷死了。。大虾给点解决方法,哪怕不用这钟方法