473,403 Members | 2,071 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,403 software developers and data experts.

FindControl is terible to use and maintain

I'm this far in determining the correct code to find a textbox I need to
set.

Me.Master.FindControl("Body1").FindControl("Form2" ).FindControl("Table2").FindControl("TableRow7").F indControl("TableCellR7C2S2").FindControl("RightCP H").FindControl("div1").FindControl("div2").FindCo ntrol("LoginView1")

Took me longer than I want to say to produce the above and I'm not there
yet.

Isn't there a better way? Seems a routine that recursively searches might
work.

So my question is: Is there a better way or do I have to keep working to
finish the above.

Thanks

PS Then if a year form now someone changes almost anything in the .aspx file
this will break!
Nov 19 '08 #1
9 2172
"AAaron123" <aa*******@roadrunner.comwrote in message
news:em**************@TK2MSFTNGP02.phx.gbl...
Isn't there a better way? Seems a routine that recursively searches might
work.
GIYF:
http://www.google.co.uk/search?sourc...trol+recursive
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 19 '08 #2
just a couple lines of code:

public static List<ControlFindControls(Control parent,
Predicate<Controlmatch)
{
var list = new List<Control>();
foreach (Control ctl in parent.Controls)
{
if (match(ctl))
list.Add(ctl);
list.AddRange(FindControls(ctl, match));
}
return list;
}

to use:

var list = FindControls(Page, c =c.ID == "myid");

-- bruce (sqlwork.com)
"AAaron123" wrote:
I'm this far in determining the correct code to find a textbox I need to
set.

Me.Master.FindControl("Body1").FindControl("Form2" ).FindControl("Table2").FindControl("TableRow7").F indControl("TableCellR7C2S2").FindControl("RightCP H").FindControl("div1").FindControl("div2").FindCo ntrol("LoginView1")

Took me longer than I want to say to produce the above and I'm not there
yet.

Isn't there a better way? Seems a routine that recursively searches might
work.

So my question is: Is there a better way or do I have to keep working to
finish the above.

Thanks

PS Then if a year form now someone changes almost anything in the .aspx file
this will break!
Nov 19 '08 #3
From the control name you used are you sure this control is not reachable
directly (ie. a variable is declared automatically for you by the designer
to access this control) ? Check the designer generated file. You have
generally to use FindControl when the control is created "dynamically" (ie
inside a repeater template, in a gridview row etc...).

Then knowing when (and perhaps what) you want to do that could be helpfull.
You could perhaps define in the markup the function that will handle a
particular event (such as the init event) so that you can work from there...

--
Patrice

"AAaron123" <aa*******@roadrunner.coma écrit dans le message de groupe de
discussion : em**************@TK2MSFTNGP02.phx.gbl...
I'm this far in determining the correct code to find a textbox I need to
set.

Me.Master.FindControl("Body1").FindControl("Form2" ).FindControl("Table2").FindControl("TableRow7").F indControl("TableCellR7C2S2").FindControl("RightCP H").FindControl("div1").FindControl("div2").FindCo ntrol("LoginView1")

Took me longer than I want to say to produce the above and I'm not there
yet.

Isn't there a better way? Seems a routine that recursively searches might
work.

So my question is: Is there a better way or do I have to keep working to
finish the above.

Thanks

PS Then if a year form now someone changes almost anything in the .aspx
file this will break!

Nov 19 '08 #4
Trying to convert to vb. Got this far but don't know what to do with c >=
c.ID = "Table2.
If you know both vb and c# I could use another push

Dim list As Generic.List(Of Control) = FindControls(Page, c >= c.ID =
"Table2")

....
Public Shared Function FindControls(ByVal parent As Control, ByVal match As
Predicate(Of Control)) As Generic.List(Of Control)

Dim list As Generic.List(Of Control) = New Generic.List(Of Control)()

For Each ctl As Control In parent.Controls

If match(ctl) Then

list.Add(ctl)

End If

list.AddRange(FindControls(ctl, match)) 'Adds the elements of the collection
ctl to the end

Next

Return list

End Function

Thanks

"bruce barker" <br*********@discussions.microsoft.comwrote in message
news:75**********************************@microsof t.com...
just a couple lines of code:

public static List<ControlFindControls(Control parent,
Predicate<Controlmatch)
{
var list = new List<Control>();
foreach (Control ctl in parent.Controls)
{
if (match(ctl))
list.Add(ctl);
list.AddRange(FindControls(ctl, match));
}
return list;
}

to use:

var list = FindControls(Page, c =c.ID == "myid");

-- bruce (sqlwork.com)
"AAaron123" wrote:
>I'm this far in determining the correct code to find a textbox I need to
set.

Me.Master.FindControl("Body1").FindControl("Form2 ").FindControl("Table2").FindControl("TableRow7"). FindControl("TableCellR7C2S2").FindControl("RightC PH").FindControl("div1").FindControl("div2").FindC ontrol("LoginView1")

Took me longer than I want to say to produce the above and I'm not there
yet.

Isn't there a better way? Seems a routine that recursively searches might
work.

So my question is: Is there a better way or do I have to keep working to
finish the above.

Thanks

PS Then if a year form now someone changes almost anything in the .aspx
file
this will break!

Nov 19 '08 #5

"Patrice" <http://www.chez.com/scribe/wrote in message
news:98**********************************@microsof t.com...
From the control name you used are you sure this control is not reachable
directly (ie. a variable is declared automatically for you by the designer
to access this control) ? Check the designer generated file. You have
generally to use FindControl when the control is created "dynamically" (ie
inside a repeater template, in a gridview row etc...).

Then knowing when (and perhaps what) you want to do that could be
helpfull. You could perhaps define in the markup the function that will
handle a particular event (such as the init event) so that you can work
from there...

--
Patrice
thanks for you interest.

I'm playing with asp:ChangePassword

It appears I can change anyone's password if I know the old password.

However the username textbox opens with my username (I logged in) in it.

No big deal to remove it but I set the ChangePassword username property to
"" and my username still appears.

Bugged me that I could not do that and I tried to find the control the hard
way.

ChangePasswordTemplate Property say there should be a "Username" control id

So, how does one find find such a control?

Thanks
Nov 19 '08 #6
Can't win. If I spend an hour looking before I ask it's not there. But if I
ask without looking it's all over the Internet.

I am glad I asked though because Bruce Barker's response included the
Predicate delegate which I looking into now.

Thanks for all the help
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:OB**************@TK2MSFTNGP03.phx.gbl...
"AAaron123" <aa*******@roadrunner.comwrote in message
news:em**************@TK2MSFTNGP02.phx.gbl...
>Isn't there a better way? Seems a routine that recursively searches might
work.

GIYF:
http://www.google.co.uk/search?sourc...trol+recursive
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 19 '08 #7
I'm not sure to understand at which step you would like to do that as I
don't know about this particular control but I was thinking about something
such as having :

<asp:ChangePassword runat="server" OnPasswordChanging="MyMethod" />

in your ASPX markup file (or for any other event that best fit your needs,
perhaps OnPreRender).

Then you don't even have to use FindControl, you take control just inside
whatever handler you want with the sender argument allowing to get the
control without having to search anything.

The same technique can apply in other cases...

--
Patrice

"AAaron123" <aa*******@roadrunner.coma écrit dans le message de groupe de
discussion : #r**************@TK2MSFTNGP03.phx.gbl...
>
"Patrice" <http://www.chez.com/scribe/wrote in message
news:98**********************************@microsof t.com...
>From the control name you used are you sure this control is not reachable
directly (ie. a variable is declared automatically for you by the
designer to access this control) ? Check the designer generated file. You
have generally to use FindControl when the control is created
"dynamically" (ie inside a repeater template, in a gridview row etc...).

Then knowing when (and perhaps what) you want to do that could be
helpfull. You could perhaps define in the markup the function that will
handle a particular event (such as the init event) so that you can work
from there...

--
Patrice

thanks for you interest.

I'm playing with asp:ChangePassword

It appears I can change anyone's password if I know the old password.

However the username textbox opens with my username (I logged in) in it.

No big deal to remove it but I set the ChangePassword username property to
"" and my username still appears.

Bugged me that I could not do that and I tried to find the control the
hard way.

ChangePasswordTemplate Property say there should be a "Username" control
id

So, how does one find find such a control?

Thanks

Nov 19 '08 #8
That sound like it would work

thanks

"Patrice" <http://www.chez.com/scribe/wrote in message
news:19**********************************@microsof t.com...
I'm not sure to understand at which step you would like to do that as I
don't know about this particular control but I was thinking about
something such as having :

<asp:ChangePassword runat="server" OnPasswordChanging="MyMethod" />

in your ASPX markup file (or for any other event that best fit your needs,
perhaps OnPreRender).

Then you don't even have to use FindControl, you take control just inside
whatever handler you want with the sender argument allowing to get the
control without having to search anything.

The same technique can apply in other cases...

--
Patrice

"AAaron123" <aa*******@roadrunner.coma écrit dans le message de groupe
de discussion : #r**************@TK2MSFTNGP03.phx.gbl...
>>
"Patrice" <http://www.chez.com/scribe/wrote in message
news:98**********************************@microso ft.com...
>>From the control name you used are you sure this control is not
reachable directly (ie. a variable is declared automatically for you by
the designer to access this control) ? Check the designer generated
file. You have generally to use FindControl when the control is created
"dynamically" (ie inside a repeater template, in a gridview row etc...).

Then knowing when (and perhaps what) you want to do that could be
helpfull. You could perhaps define in the markup the function that will
handle a particular event (such as the init event) so that you can work
from there...

--
Patrice

thanks for you interest.

I'm playing with asp:ChangePassword

It appears I can change anyone's password if I know the old password.

However the username textbox opens with my username (I logged in) in it.

No big deal to remove it but I set the ChangePassword username property
to "" and my username still appears.

Bugged me that I could not do that and I tried to find the control the
hard way.

ChangePasswordTemplate Property say there should be a "Username" control
id

So, how does one find find such a control?

Thanks


Nov 19 '08 #9
its a lambda expression being used as a delgate. i don't know the vb syntax,
use an anonymous delegate or a lookup the lambda syntax.

-- bruce (sqlwork.com)
"AAaron123" wrote:
Trying to convert to vb. Got this far but don't know what to do with c >=
c.ID = "Table2.
If you know both vb and c# I could use another push

Dim list As Generic.List(Of Control) = FindControls(Page, c >= c.ID =
"Table2")

....
Public Shared Function FindControls(ByVal parent As Control, ByVal match As
Predicate(Of Control)) As Generic.List(Of Control)

Dim list As Generic.List(Of Control) = New Generic.List(Of Control)()

For Each ctl As Control In parent.Controls

If match(ctl) Then

list.Add(ctl)

End If

list.AddRange(FindControls(ctl, match)) 'Adds the elements of the collection
ctl to the end

Next

Return list

End Function

Thanks

"bruce barker" <br*********@discussions.microsoft.comwrote in message
news:75**********************************@microsof t.com...
just a couple lines of code:

public static List<ControlFindControls(Control parent,
Predicate<Controlmatch)
{
var list = new List<Control>();
foreach (Control ctl in parent.Controls)
{
if (match(ctl))
list.Add(ctl);
list.AddRange(FindControls(ctl, match));
}
return list;
}

to use:

var list = FindControls(Page, c =c.ID == "myid");

-- bruce (sqlwork.com)
"AAaron123" wrote:
I'm this far in determining the correct code to find a textbox I need to
set.

Me.Master.FindControl("Body1").FindControl("Form2" ).FindControl("Table2").FindControl("TableRow7").F indControl("TableCellR7C2S2").FindControl("RightCP H").FindControl("div1").FindControl("div2").FindCo ntrol("LoginView1")

Took me longer than I want to say to produce the above and I'm not there
yet.

Isn't there a better way? Seems a routine that recursively searches might
work.

So my question is: Is there a better way or do I have to keep working to
finish the above.

Thanks

PS Then if a year form now someone changes almost anything in the .aspx
file
this will break!


Nov 19 '08 #10

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

Similar topics

1
by: James G. Beldock | last post by:
I have seen the following behavior: when issuing a Page.FindControl() for a control which exists in an item template (from within an ItemDataBound() event, for example), I get nulls back...
2
by: christof | last post by:
How to do it: My page: <asp:DataList ID="dataListRoleMembers" ...> .... <FooterTemplate> <asp:LinkButton ID="btnAddMember" runat="server"...
2
by: encoad | last post by:
Hi everyone, I'm slowly going mad with Masterpages and the FindControl. After a couple days of figuring out how to use the FindControl command from within a Masterpage, I still can't explain...
11
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
I am trying to get the text of an item in a GridView, but am doing something wrong. Can someone help me with the correct C# statement I need? Below is my GridView and my attempt to get the control....
5
by: daniel.hedz | last post by:
I am generating a usercontrol dynamically successfully, but when I try to find that usercontrol I get a type mismatch. This is what I am doing: //Loading my usercontrol...
1
by: Dan | last post by:
Hi, I created a custom control (ParentCustomControl) which is using a custom template (implementing ITemplate interface), in the instantiateIn method of this template I create all the controls I...
14
by: =?Utf-8?B?QWxleCBNYWdoZW4=?= | last post by:
Hi. I have created a UserControl ("MyUC"). I've put a bunch of instances of that control on a Page ("Defaul.aspx"). The control works fine. Now, I want to be able to use "FindControl()" from...
7
by: AAaron123 | last post by:
Me.FindControl("MissionScheduleID"), below returns null. Do you know what I'm doing wrong? Thanks ***In my .aspx file I have: asp:Content ID="Content3"...
4
by: Hillbilly | last post by:
Maybe this is or isn't some kind of bug but it sure is goofy and remains a mystery that really has me puzzled for two reasons... // goofy syntax functions as expected... Panel finalStepButton =...
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: 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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.