Page.FindControl

  • Thread starter Thread starter Rob Meade
  • Start date Start date
R

Rob Meade

Hi all,

I was wondering if you can help. I have the need to find a control on the
page for which I don't know all of the ID, this is because it is made up
from several id's forming one new id. Is there anyway to find this control
with only part of the information (the part I have will definately be
unique).

I was hoping that it might be possible to push a regular expression in the
string part of the Page.FindControl - but I dont think I can.

I've tried iterating through all the controls on the page, but - well - it
lies! If I add Page.Controls to the watch it says there's five, if I then
add .Item(3) - the html form control, and then add .Controls this is full of
more controls...

Can anyone help me?

Regards

Rob
 
Rob said:
Hi all,

I was wondering if you can help. I have the need to find a control
on the page for which I don't know all of the ID, this is because it
is made up from several id's forming one new id. Is there anyway to
find this control with only part of the information (the part I have
will definately be unique).

I was hoping that it might be possible to push a regular expression
in the string part of the Page.FindControl - but I dont think I can.

I've tried iterating through all the controls on the page, but - well
- it lies! If I add Page.Controls to the watch it says there's five,
if I then add .Item(3) - the html form control, and then add
.Controls this is full of more controls...

You have to make a recursive loop.

I think that's the only solution here.
 
Hi Rob,
FindControl method is always related to current Naming container. So if you
want to use plain ID as it is defined you will need to have reference to
control which is naming container for controls you are looking for.

You can also use whole unique ID from top level naming container (usually
page) or part of unique ID - but again you have to relate it to proper naming
container.

If unique ID of your control is built from several IDs it means it is in
hierarchy of naming containers. Each container will add its id followed by
separator (usually $) to final id of your control.

Say you have control with uniqueID ctl00$ctl00$ctl00$myLabel. You can find
this control by calling Page.FindControl("ctl00$ctl00$ctl00$myLabel") or if
you have reference to control ctl00$ctl00 (named for example myGridRow) you
can call myGridRow.FindControl("ctl00$myLabel").

Use tracing to see whole tree of controls including naming containers.

Regards,
Ladislav
 
...
Say you have control with uniqueID ctl00$ctl00$ctl00$myLabel. You can find
this control by calling Page.FindControl("ctl00$ctl00$ctl00$myLabel") or
if
you have reference to control ctl00$ctl00 (named for example myGridRow)
you
can call myGridRow.FindControl("ctl00$myLabel").

Use tracing to see whole tree of controls including naming containers.

Hi Ladislav,

Thanks for the reply.

I should have been more clear in my post, its actually me making the
multi-part id's for the controls, not .Net because they are in other
controls etc....

I am using various ID's from my database content and using them in the id's
of the controls.

Thanks for the information though - very useful.

Regards

Rob
 
Back
Top