FindControl is terible to use and maintain

A

AAaron123

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("RightCPH").FindControl("div1").FindControl("div2").FindControl("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!
 
B

bruce barker

just a couple lines of code:



public static List<Control> FindControls(Control parent,
Predicate<Control> match)
{
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)
 
A

AAaron123

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
 
A

AAaron123

Patrice said:
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...

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
 
A

AAaron123

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
 
A

AAaron123

That sound like it would work

thanks

Patrice said:
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...
 
B

bruce barker

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)
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top