FileUpload file=(FileUpload)DataList1.FindControl("FileUpload1");
        file = new FileUpload();
        
        if (file.PostedFile != null && file.PostedFile.ContentLength != 0)
        {            int size = file.PostedFile.ContentLength;
            my_DataBase.SqlConnection_Open();
            string fileName = file.PostedFile.FileName; //获取初始文件名   
            int i = fileName.LastIndexOf(".");                      //取得文件名中最后一个"."的索引   
            string newName = fileName.Substring(i);                 //获取文件扩展名   
            if (newName != ".gif" && newName != ".jpg" && newName != ".jpeg" && newName != ".bmp" && newName != ".png" && newName != ".wmf")//限制文件格式
            {
                Response.Write("<script>alert('图片格式不正确!')");
                Response.End();
            }
            string pathName = DateTime.Now.ToString("yyMMddhhmmss") + newName; //重新为文件命名(),文件大小+扩展名,此文件名根据情况可有可无 
            string fp = "UpFile/" + pathName;
            file.PostedFile.SaveAs(Server.MapPath(fp)); //上传后保存的地方 一般是服务器ip地址+文件名 
            String SqlCmd = "update  [File]  set fileName=@fileName,fileType=@fileType,fileSize=@fileSize,fileContext=@fileContext where ID='"+pid+"'";
            SqlCommand CmdObj = new SqlCommand(SqlCmd, my_DataBase.conn);
            CmdObj.Parameters.Add("@fileName", SqlDbType.VarChar, 100).Value = pathName;
            CmdObj.Parameters.Add("@fileType", SqlDbType.VarChar, 4000).Value = file.PostedFile.ContentType;
            //把其它单表数据记录上传
            CmdObj.Parameters.Add("@fileSize", SqlDbType.VarChar, 4000).Value = size;
            CmdObj.Parameters.Add("@fileContext", SqlDbType.VarChar, 50).Value = fp;
            CmdObj.ExecuteNonQuery();
            my_DataBase.connection_close();
            Response.Write("<script>alert('OK');</script>");
        }
        else
        {
            Response.Write("<script>alert('无法上传空文件');</script>");
        }

解决方案 »

  1.   

    不知道为什么findcontrol之后还要使用new新建一对象,不过在datalist模板里找控件是要在Item里找的
    类似FileUpload file=(FileUpload)DataList1.Items[0].FindControl("FileUpload1"); 
      

  2.   

    FileUpload file=(FileUpload)DataList1.FindControl("FileUpload1"); 
            file = new FileUpload(); 
            
            if (file.PostedFile != null && file.PostedFile.ContentLength != 0) 
            { 
    你这样取到值才怪呢!