FormView and FindControl behaviour

Discussion in 'ASP .Net' started by J055, Nov 30, 2007.

  1. J055

    J055 Guest

    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
    J055, Nov 30, 2007
    #1
    1. Advertising

  2. 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


    "J055" <> wrote in message
    news:...
    > 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
    >
    Scott Roberts, Nov 30, 2007
    #2
    1. Advertising

  3. 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 (, 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.
    Walter Wang [MSFT], Dec 3, 2007
    #3
  4. J055

    Lloyd Sheen Guest

    ""Walter Wang [MSFT]"" <> wrote in message
    news:...
    > 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 (, 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
    Lloyd Sheen, Dec 3, 2007
    #4
  5. J055

    tlbignerd Guest

    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

    Lloyd Sheen wrote:
    > *""Walter Wang [MSFT]"" <> wrote in
    > message
    > news:...
    > > 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 (, 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 *




    --
    tlbignerd
    ------------------------------------------------------------------------
    Posted via http://www.codecomments.com
    ------------------------------------------------------------------------
    tlbignerd, Jan 30, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zarn Bou via DotNetMonster.com

    FindControl in FormView

    Zarn Bou via DotNetMonster.com, Mar 16, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    2,871
    Zarn Bou via DotNetMonster.com
    Mar 16, 2005
  2. Paul Aspinall

    FormView - FindControl

    Paul Aspinall, Feb 25, 2006, in forum: ASP .Net
    Replies:
    14
    Views:
    38,578
  3. sck10
    Replies:
    2
    Views:
    5,031
    sck10
    Aug 28, 2006
  4. jobs
    Replies:
    0
    Views:
    401
  5. Mr. SweatyFinger

    findcontrol in a formview

    Mr. SweatyFinger, Dec 1, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    430
    Mr. SweatyFinger
    Dec 1, 2006
Loading...

Share This Page