Another user had this question before, but without any success of getting an answer. I would like to know how one can loop through a few FileUpload controls within a Formview. Say I want to loop through these and retrieve the FileBytes of each of the files inputed in these FileUpload controls. Any suggestions and thoughts would definitaly make me smarter right now.. feel like I'm in a dead-end.
Currently using this code:
foreach (Control pin Page.Controls) {if (pisFileUpload) {// do the retrieving of filebytes
} }
Thx in advance.
try something like this:
foreach (Control ctrlin this.FormView1.Controls) {if ((ctrlis FileUpload)) {//cast the ctrl to a FileUpload control FileUpload fileUp =(FileUpload) ctrl;if (fileUp.HasFile) {// you can now access the file stream with fileUp.FileContent;// or save the file with this fileUp.SaveAs(); } }}thanks for replying mbanavige,
I tried the above code, and it seems logical to do this. I debugged it and it seems like it doesn't recognize the FileUpload controls. Can it be that the process of looping through controls are different for FileUpload control? or is it the same for all of them?
apologies. due to the way that the FormView nests the child controls, we need to dig a little deeper to get at the controls in your item template:
// FormView1.Row = the data row// FormView1.Row.Controls(0) = the 1st tablecell in the row// FormView1.Row.Controls(0).Controls = the control collection in that cellforeach (Control ctrlin this.FormView1.Row.Controls(0).Controls) {if ((ctrlis FileUpload)) {// cast the ctrl to a FileUpload control FileUpload fileUp = ((FileUpload)(ctrl));if (fileUp.HasFile) {// you can now access the file stream with // fileUp.FileContent // or save the file with this // fileUp.SaveAs() } }}mbanavige my friend, that's excellent! I got an error telling me thatControls(0) was a property but used as a method, but that was mainly because we had to use brackets there instead of parenthesis because it's an array.
Thank you very much again! Thumbs up for you.
/Arre S.
0 comments:
Post a Comment