473,378 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

find control question

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("Conte ntPlaceHolder1");

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
Aug 4 '08 #1
7 1360
"WebBuilder451" <We***********@discussions.microsoft.comwrote in message
news:A6**********************************@microsof t.com...
They come up null
Where are the controls located??
Are they located within another container, maybe an <asp:Panel /...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 4 '08 #2

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
"Mark Rae [MVP]" wrote:
"WebBuilder451" <We***********@discussions.microsoft.comwrote in message
news:A6**********************************@microsof t.com...
They come up null
Where are the controls located??

Are they located within another container, maybe an <asp:Panel /...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 4 '08 #3
"WebBuilder451" <We***********@discussions.microsoft.comwrote in message
news:9B**********************************@microsof t.com...

[top-posting corrected]
>>>They come up null
Where are the controls located??

Are they located within another container, maybe an <asp:Panel /...?

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?sourc...trol+recursive
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 4 '08 #4
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<ControlGetAListOfControls(Control root, Type
typ, List<ControlControlList)
{
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<Controlctrls = new List<Control>();
ctrls = AdminPageHelper.GetAListOfControls(cph, typeof(TextBox), ctls);

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

--

"WebBuilder451" wrote:
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("Conte ntPlaceHolder1");

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
Aug 4 '08 #5
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
"Yankee Imperialist Dog" wrote:
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<ControlGetAListOfControls(Control root, Type
typ, List<ControlControlList)
{
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<Controlctrls = new List<Control>();
ctrls = AdminPageHelper.GetAListOfControls(cph, typeof(TextBox), ctls);

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

--

"WebBuilder451" wrote:
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("Conte ntPlaceHolder1");

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
Aug 4 '08 #6
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 [MVP]" wrote:
"WebBuilder451" <We***********@discussions.microsoft.comwrote in message
news:9B**********************************@microsof t.com...

[top-posting corrected]
>>They come up null
Where are the controls located??

Are they located within another container, maybe an <asp:Panel /...?
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?sourc...trol+recursive
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 4 '08 #7
then try this routine:

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

....

public List<ControlControlWalker(
Control ctl,
Predicate<Controlmatcher)
{
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[i],matcher);
if (childList.Count 0)
list.AddRange(childList);
}
return (list);
}

-- bruce (sqlwork.com)
"WebBuilder451" wrote:
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
"Yankee Imperialist Dog" wrote:
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<ControlGetAListOfControls(Control root, Type
typ, List<ControlControlList)
{
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<Controlctrls = new List<Control>();
ctrls = AdminPageHelper.GetAListOfControls(cph, typeof(TextBox), ctls);

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

--

"WebBuilder451" wrote:
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("Conte ntPlaceHolder1");
>
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
Aug 4 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Yasutaka Ito | last post by:
Hi, Is there a way to find to which control the current control is docked against? For example, let's say I have panel1 and panel2 docked to left within a form. The panel1 is the first one on...
5
by: Dan | last post by:
Hi, I'd like to find out the control that caused a postback to be raised. Obviously this could simply done in a control event handler. I am not going to do this method and would like no...
2
by: JollyK | last post by:
Hi friends, This is my question.... From the Page Load event (or Page Init event), I would need to find which event had occurred that caused a PostBack, for example was it a event fired from...
5
by: sck10 | last post by:
Hello, I am using the code below to set the values of a DetailsView template field using FindControl. My question is how would you find a control if its a Boundfield control? For example,...
5
by: Marc Robitaille | last post by:
Hello, Is there better a way of finding a control on WinForm than to use a recursive method on the controls collection property? I ask this question because I have winforms which have many...
0
by: mschep | last post by:
Hi, I built an assembly with a set of user controls. This can be done with the Visual Studio 2005 Deployment Project: building and merging for example all your aspx and ascx in one dll (lets...
9
by: Tony Girgenti | last post by:
Hello. I'm developing and testing a web application using VS.NET 2003, VB, .NET Framework 1.1.4322, ASP.NET 1.1.4322 and IIS5.1 on a WIN XP Pro, SP2 computer. I'm using a web form. For a...
1
by: =?Utf-8?B?aUhhdkFRdWVzdGlvbg==?= | last post by:
I am using a menu control in which i have a tab controld in which i have a dropdownlilst How do i find the values in a control. I have tried this in C# ?LTCMenu1.FindControl] Can any one...
6
by: DC | last post by:
Hi, our cms (asp.net 2.0) dynamically inserts controls into asp.net pages, sometimes 50 and more per page, and if we run into a performance bottleneck we have a hard time finding out which...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.