FindControl

J

Jim McGivney

I am trying to form a string by concatenating the Text strings of a number
of Label controls. The Labels are Label33 to Label64. I thought I would
use a For Loop to increment through the labels. The code below does not
work as Text is not a recognized property of myControl, which is the name of
the control fetched by FindControl. Any suggestions as to how to use
FindControl to obtain the Text value of the found control would be
appreciated.
Thanks,
Jim

protected void myButton_Click(object sender, EventArgs e)
{
string MyInfo = "";
string Indx;
int i;
Control myControl;
for (i = 1; i < 32; i += 1)
{
Indx = "Label" + i.ToString();
myControl = FindControl(Indx);
MyInfo += myControl.Text;
}
do something here = MyInfo;
}
 
A

Artur Borecki

Jim McGivney said:
I am trying to form a string by concatenating the Text strings of a number
of Label controls. The Labels are Label33 to Label64. I thought I would
use a For Loop to increment through the labels. The code below does not
work as Text is not a recognized property of myControl, which is the name
of the control fetched by FindControl. Any suggestions as to how to use
FindControl to obtain the Text value of the found control would be
appreciated.
Thanks,
Jim

protected void myButton_Click(object sender, EventArgs e)
{
string MyInfo = "";
string Indx;
int i;
Control myControl;
for (i = 1; i < 32; i += 1)
{
Indx = "Label" + i.ToString();
myControl = FindControl(Indx);
MyInfo += myControl.Text;
}
do something here = MyInfo;
}


FindControl returns base class of all controls , namely "Control".
You need to cast returned value to appropriate class, in you case to Label:
 
J

Jim McGivney

Art:
The code still doesn't compile. I get a message that myControl does not
contain a definition for "Text"
Jim
 
A

Artur Borecki

Jim McGivney said:
Art:
The code still doesn't compile. I get a message that myControl does not
contain a definition for "Text"
Jim

Did you changed declaration of myControl variable from
Control myControl;
to
Label myControl; ???
 
J

Jim McGivney

Art:
Thanks, it worked like a charm.
Thanks,
Jim

Artur Borecki said:
Did you changed declaration of myControl variable from
Control myControl;
to
Label myControl; ???
 

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