find control question

W

WebBuilder451

i have a master page and aseries of controls labeled fn1, fn2, fn3 , ...

i want ot loop and use find control but i'm finding them.
i'm using the following w/o luck

ContentPlaceHolder cph =
(ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");

for (int i = 0; i < 7; i++)
{
TextBox tbF = (TextBox)cph.FindControl("tbFN" + i.ToString());
TextBox tbL = (TextBox)cph.FindControl("tbLN" + i.ToString());

.........
}

they come up null
where are the controls located??
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
 
W

WebBuilder451

Appreciate the response!
no just an HTML table.
i have to be doing something odd for this not to be straight forward

if i wanted to use findcontrol even though i don't need to us shouldn't it
just work?

the object Page.Master.NamingContainer gives me a list of the controls in
debug
i'd think a simple findControl should work?
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
 
Y

Yankee Imperialist Dog

total bummer here with this i, know! My solution is total over kill here, but
it works

ok, use generics to return a list of controls.
i use this in a name space, but you could just use it for a simple
/// <summary>
/// returns a list of the given controls
/// </summary>
/// <param name="root"></param>
/// <param name="typ"></param>
/// <param name="ControlList"></param>
/// <returns></returns>
public static List<Control> GetAListOfControls(Control root, Type
typ, List<Control> ControlList)
{
foreach (Control t in root.Controls)
{
if (t.GetType() == typ)
ControlList.Add(t);
if (t.Controls.Count > 0)
{
ControlList = GetAListOfControls(t, typ, ControlList);
}
}
return ControlList;
}

then call it thus for the texh boxes:

List<Control> ctrls = new List<Control>();
ctrls = AdminPageHelper.GetAListOfControls(cph, typeof(TextBox), ctls);

from there you got the list loop or use as you see fit.
 
W

WebBuilder451

a lot of work for a simple task. But i'll try it!
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
 
W

WebBuilder451

ok, thanks i'll check them out

--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes


Mark Rae said:
[top-posting corrected]
No just an HTML table.

As I thought... The answer is, therefore, "Yes, they are located within an
HTML table"... ;-)
I have to be doing something odd for this not to be straight forward

Nope - what you are seeing is standard behaviour.
If I wanted to use FindControl even though I don't need to us shouldn't it
just work?

It is working precisely as it was designed, not the way you think it
should... :)

FindControl is not recursive - if you tell it to find a control in a page,
it will find only those controls which are directly contained within the
Page container, not those which are contained in child containers.

There are loads of workarounds for this:
http://www.google.co.uk/search?sour...BGB252GB252&q="ASP.NET"+FindControl+recursive
 
B

bruce barker

then try this routine:

var list = ControlWalker(this, ctl =>
ctl.ID.StartsWith("tbFN") ||
ctl.ID.StartsWith("tbLN"));

....

public List<Control> ControlWalker(
Control ctl,
Predicate<Control> matcher)
{
var list = new List<Control>();
if (matcher(ctl)) list.Add(ctl);
for (int i=0; i < ctl.Controls.Count; ++i)
{
var childList = ControlWalker(
ctl.Controls,matcher);
if (childList.Count > 0)
list.AddRange(childList);
}
return (list);
}



-- bruce (sqlwork.com)
 

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

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top