FormView and FindControl behaviour

J

J055

Hi

I can't figure out what the difference is here. Sometimes I have a simple
FormView control in a page and I can use the FormView.FindControl method to
get a Label or TextBox with in it during the DataBound event. Then I can use
a FormView somewhere else and the FindControl method can't find any of the
inner controls. I get a NullReferenceException.

I always check to see which Template is loaded first. I know that if you
loop through the FormView controls you get a ChildTable first but why can I
use FindControl sometimes to return an object but not others?

Thanks
Andrew
 
S

Scott Roberts

Hi Andrew,

We use the FormView fairly regularly and the only problem I recall having
(off the top of my head) is when looking for controls that aren't on the
"current" template. It sounds like you've already covered that, but you may
want to double-check.

I assume you are also aware that FindControl is not recursive, so if the
TextBox or Label is inside a Panel or something it won't be found.

Scott
 
W

Walter Wang [MSFT]

Hi Andrew,

As Scott described, FindControl isn't finding controls recursively. It can
only find controls in the same naming container. A control acts as a naming
container when it implements INamingContainer interface. A template
normally implements INamingContainer.

To dermine which control exists in which naming container, turn on "Trace"
option in the @ page directive, then you will the control hierarchy when
the page renders.

Maybe you could show us an example when you're able to use FindControl on
FormView directly and when you're not able to, we can use the code to
explain why it works (or doesn't work).

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Lloyd Sheen

"Walter Wang [MSFT]" said:
Hi Andrew,

As Scott described, FindControl isn't finding controls recursively. It can
only find controls in the same naming container. A control acts as a
naming
container when it implements INamingContainer interface. A template
normally implements INamingContainer.

To dermine which control exists in which naming container, turn on "Trace"
option in the @ page directive, then you will the control hierarchy when
the page renders.

Maybe you could show us an example when you're able to use FindControl on
FormView directly and when you're not able to, we can use the code to
explain why it works (or doesn't work).

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

If I can add to what Walter said, I would advise getting the IE Developers
Toolbar. You can use this to see easily the ID's of controls etc. There is
a "click" feature which allows you to click on a control and it will then
show you all the info for that control.

Hope this helps
Lloyd Sheen
 
T

tlbignerd

I've been having the same problem. In our specific case we display the
edit mode by default and bind the data based on a parameter passed into
the datasource. If no record is returned (usually we return a blank row
so the edit form acts as an add form) then we came across the problem
that no formview elements would exist and we couldn't find the
controls.

For some reason, as soon as it tests for the control and doesn't find
it, the formview becomes a null reference exception instead of showing
that the control does not exist.

I hope this helps.

+T
 

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

Similar Threads

FormView FindControl error 1
FormView - FindControl 14
FormView 3
FormView FindControl issue 1
findcontrol behaviour 1
FindControl in FormView 0
Get Data value in FormView 5
FindControl in FormView Edit Template 1

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top