Stop ForEach Loop and Control Reference Problems

G

Guest

I am having a problem stopping a loop and making reference to a control. I
receive errors regarding not being able to find the control and can not stop
the loop. The following is the code I am using (while this code may not
make logical sense, it is necessary that I use it this way).

foreach (Control repeaterControls in repeaterFields.Controls)
{
foreach (Control childc in repeaterControls.Controls)
{
foreach (Control ctrl in childc.Controls)
{
if (ctrl.ID == "aaaa")
{
FileUpload fuCtrl =
(FileUpload)childc.FindControl(((FileUpload)ctrl).ID.ToString());
if (fuCtrl.PostedFile.FileName.ToString().Trim().Length > 0)
{
//use this value as the value passed to UseThisvalue()
then end this loop meaning do not go to the hidden field block
UseThisValue(fuCtrl.PostedFile.FileName.ToString());
//need to end here and not go to the hidden field block
because there is something in the upload control textbox
}
}
if (ctrl.ID == "aaaaBBBB")
{
HiddenField hdnTheString =
(HiddenField)childc.FindControl(((HiddenField)ctrl).ID.ToString());
if (hdnTheString.Value.ToString().Trim().Length > 0)
{
if (fuCtrl.PostedFile.FileName.ToString().Trim().Length
== 0)
{
// use the value from the hidden field because there is nothing in the
file upload path
UseThisValue(hdnTheString.Value.ToString())
//because there is nothing in the fileupload text
use this value
}
}
}
}
}
}


The problem is that within the hiddenfield loop the code cannot see the
fileupload control and determine if there is text in it. While looping I
need to determine if there is text in the fileupload control and if so, use
that text, NOT the value in the hidden field. However, if and only if there
is no value in the fileupload control I need to use the value in the hidden
field. These values are passed to another function but I only need the one
value. Errors I'm receiving are regarding finding the fileupload within the
hiddenfield loop. I also cannot seem to stop the loop after finding a value
in the fileupload control. Any comments would be greatly appreciated.
 
H

Hans Kesting

1) change the scope of the fuCtrl variable by declaring it outside your
loops
2) the "continue" keyword skips the rest of the loop-processing and
continues with the next iteration.

see changes inline:

msnews.microsoft.com submitted this idea :
I am having a problem stopping a loop and making reference to a control. I
receive errors regarding not being able to find the control and can not stop
the loop. The following is the code I am using (while this code may not make
logical sense, it is necessary that I use it this way).
FileUpload fuCtrl;
 
G

Guest

Thanks a lot for the reply. I still get "use of unassigned local variable"
error at if (fuCtrl.PostedFile.FileName.ToString().Trim().Length > 0) line
in hidden field block.
 
H

Hans Kesting

msnews.microsoft.com wrote on 8-4-2008 :
Thanks a lot for the reply. I still get "use of unassigned local variable"
error at if (fuCtrl.PostedFile.FileName.ToString().Trim().Length > 0) line in
hidden field block.

change the declaration (FileUpload fuCtrl;) to
FileUpload fuCtrl = null;

NOTE: I forgot to mention that you need to be sure that everything
works in the correct order. *is* the value of fuCtrl really the
FileUpload you want when you process that hidden field?
With the particular page you use, that might be guaranteed, just don't
change the order of the controls.

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top