FindControl in Class

Discussion in 'ASP .Net' started by MRW, Sep 4, 2006.

  1. MRW

    MRW Guest

    Hello!

    I have a problem, I can't seem to solve.

    I'm making a class in my page that will hold the members of a FormView,
    so I can access them freely throughout the several functions and
    subroutines, without having to redefine them every function/subroutine.

    I ran into a problem when trying to define a GridView in that FormView
    and I was hoping somebody can help me on this, since I'm going a tad
    crazy. Here's part of the code:

    ---
    Partial Public Class FM
    Inherits System.Web.UI.Page

    Public FV_Customer As FormView
    Public GV_PhoneNumbers As GridView =
    FV_Customer.FindControl("GV_PhoneNumbers")

    ---

    I keep getting an "Object reference not set to an instance of an
    object. " for the GridView. Can anybody help?

    Thanks in advance!
    MRW, Sep 4, 2006
    #1
    1. Advertising

  2. MRW

    RHIZOME Guest

    Declare new with Public FV_Customer As FormView

    e.g. Public FV_Customer As New FormView



    MRW wrote:
    > Hello!
    >
    > I have a problem, I can't seem to solve.
    >
    > I'm making a class in my page that will hold the members of a FormView,
    > so I can access them freely throughout the several functions and
    > subroutines, without having to redefine them every function/subroutine.
    >
    > I ran into a problem when trying to define a GridView in that FormView
    > and I was hoping somebody can help me on this, since I'm going a tad
    > crazy. Here's part of the code:
    >
    > ---
    > Partial Public Class FM
    > Inherits System.Web.UI.Page
    >
    > Public FV_Customer As FormView
    > Public GV_PhoneNumbers As GridView =
    > FV_Customer.FindControl("GV_PhoneNumbers")
    >
    > ---
    >
    > I keep getting an "Object reference not set to an instance of an
    > object. " for the GridView. Can anybody help?
    >
    > Thanks in advance!
    RHIZOME, Sep 4, 2006
    #2
    1. Advertising

  3. MRW

    MRW Guest

    What the???

    Mmm...

    Okay... can you explain exactly what that "New" does that made it work?

    Thanks for the help!

    RHIZOME wrote:
    > Declare new with Public FV_Customer As FormView
    >
    > e.g. Public FV_Customer As New FormView
    >
    >
    >
    > MRW wrote:
    > > Hello!
    > >
    > > I have a problem, I can't seem to solve.
    > >
    > > I'm making a class in my page that will hold the members of a FormView,
    > > so I can access them freely throughout the several functions and
    > > subroutines, without having to redefine them every function/subroutine.
    > >
    > > I ran into a problem when trying to define a GridView in that FormView
    > > and I was hoping somebody can help me on this, since I'm going a tad
    > > crazy. Here's part of the code:
    > >
    > > ---
    > > Partial Public Class FM
    > > Inherits System.Web.UI.Page
    > >
    > > Public FV_Customer As FormView
    > > Public GV_PhoneNumbers As GridView =
    > > FV_Customer.FindControl("GV_PhoneNumbers")
    > >
    > > ---
    > >
    > > I keep getting an "Object reference not set to an instance of an
    > > object. " for the GridView. Can anybody help?
    > >
    > > Thanks in advance!
    MRW, Sep 4, 2006
    #3
  4. MRW

    Teemu Keiski Guest

    If you have members on aspx page, and you reference them, you *never* create
    new instances of them yourself, unless you also add them to the Controls
    collection e.g create dynamical controls. If control is declared on aspx,
    page parser takes care of providing the plumbing to instantiate a control.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    "RHIZOME" <> wrote in message
    news:...
    > Declare new with Public FV_Customer As FormView
    >
    > e.g. Public FV_Customer As New FormView
    >
    >
    >
    > MRW wrote:
    >> Hello!
    >>
    >> I have a problem, I can't seem to solve.
    >>
    >> I'm making a class in my page that will hold the members of a FormView,
    >> so I can access them freely throughout the several functions and
    >> subroutines, without having to redefine them every function/subroutine.
    >>
    >> I ran into a problem when trying to define a GridView in that FormView
    >> and I was hoping somebody can help me on this, since I'm going a tad
    >> crazy. Here's part of the code:
    >>
    >> ---
    >> Partial Public Class FM
    >> Inherits System.Web.UI.Page
    >>
    >> Public FV_Customer As FormView
    >> Public GV_PhoneNumbers As GridView =
    >> FV_Customer.FindControl("GV_PhoneNumbers")
    >>
    >> ---
    >>
    >> I keep getting an "Object reference not set to an instance of an
    >> object. " for the GridView. Can anybody help?
    >>
    >> Thanks in advance!

    >
    Teemu Keiski, Sep 4, 2006
    #4
  5. MRW

    Teemu Keiski Guest

    Perhaps the FormView is in another mode when running the FindControl
    compared to what it's in when you show it? E.g Edit, Insert, ReadOnly



    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    "MRW" <> wrote in message
    news:...
    > Hello!
    >
    > I have a problem, I can't seem to solve.
    >
    > I'm making a class in my page that will hold the members of a FormView,
    > so I can access them freely throughout the several functions and
    > subroutines, without having to redefine them every function/subroutine.
    >
    > I ran into a problem when trying to define a GridView in that FormView
    > and I was hoping somebody can help me on this, since I'm going a tad
    > crazy. Here's part of the code:
    >
    > ---
    > Partial Public Class FM
    > Inherits System.Web.UI.Page
    >
    > Public FV_Customer As FormView
    > Public GV_PhoneNumbers As GridView =
    > FV_Customer.FindControl("GV_PhoneNumbers")
    >
    > ---
    >
    > I keep getting an "Object reference not set to an instance of an
    > object. " for the GridView. Can anybody help?
    >
    > Thanks in advance!
    >
    Teemu Keiski, Sep 4, 2006
    #5
  6. MRW

    MRW Guest

    I see...

    Then how do I work it so I can have all my controls and datasets
    accessible by all my functions and subroutines on that page, without
    having to redefine them.

    For example, if I have a control, or let's go with a dataset called ds,
    how can I fill it out in my onLoad, then run a subroutine that uses and
    manipulates it.

    Right now I have:

    Partial Public Class FM
    Inherits System.Web.UI.Page

    Public ds As New Data.DataSet...
    ...
    End Class

    Then on every subroutine, I would have:

    Sub fillout()
    Dim FM As FM
    FM.ds ...
    End Sub

    I simply want to be able to use my dataset and controls without
    redefining them at every subroutine.

    Thanks for any help!

    Teemu Keiski wrote:
    > If you have members on aspx page, and you reference them, you *never* create
    > new instances of them yourself, unless you also add them to the Controls
    > collection e.g create dynamical controls. If control is declared on aspx,
    > page parser takes care of providing the plumbing to instantiate a control.
    >
    > --
    > Teemu Keiski
    > ASP.NET MVP, AspInsider
    > Finland, EU
    > http://blogs.aspadvice.com/joteke
    >
    >
    MRW, Sep 4, 2006
    #6
  7. MRW

    Teemu Keiski Guest

    You pass them as arguments to the function (or take them in class
    constructor) since DataSet is a reference type.

    You can have the method like this (assuming static here, you could also have
    instantiated class in case usage is different)

    Public Class DoesSomething

    Public Shared Sub fillout(dsArgument As DataSet)
    'Using dsArguments
    End Sub

    End Class

    when in the code-behind you'd just have

    Partial Public Class FM
    Inherits System.Web.UI.Page

    Public ds As New Data.DataSet...

    Protected Sub Page_Load(sender as Object, e As EventArgs)
    'call the method with ds as argument
    DoesSomething.fillout(ds)
    End Sub

    End Class


    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke





    "MRW" <> wrote in message
    news:...
    >I see...
    >
    > Then how do I work it so I can have all my controls and datasets
    > accessible by all my functions and subroutines on that page, without
    > having to redefine them.
    >
    > For example, if I have a control, or let's go with a dataset called ds,
    > how can I fill it out in my onLoad, then run a subroutine that uses and
    > manipulates it.
    >
    > Right now I have:
    >
    > Partial Public Class FM
    > Inherits System.Web.UI.Page
    >
    > Public ds As New Data.DataSet...
    > ...
    > End Class
    >
    > Then on every subroutine, I would have:
    >
    > Sub fillout()
    > Dim FM As FM
    > FM.ds ...
    > End Sub
    >
    > I simply want to be able to use my dataset and controls without
    > redefining them at every subroutine.
    >
    > Thanks for any help!
    >
    > Teemu Keiski wrote:
    >> If you have members on aspx page, and you reference them, you *never*
    >> create
    >> new instances of them yourself, unless you also add them to the Controls
    >> collection e.g create dynamical controls. If control is declared on aspx,
    >> page parser takes care of providing the plumbing to instantiate a
    >> control.
    >>
    >> --
    >> Teemu Keiski
    >> ASP.NET MVP, AspInsider
    >> Finland, EU
    >> http://blogs.aspadvice.com/joteke
    >>
    >>

    >
    Teemu Keiski, Sep 5, 2006
    #7
    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. George Durzi

    OnEditCommand - .FindControl Returning Null

    George Durzi, Jul 14, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    476
    George Durzi
    Jul 14, 2003
  2. E11
    Replies:
    1
    Views:
    4,700
    Thomas Weidenfeller
    Oct 12, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    730
    Pete Becker
    May 4, 2005
  4. Joseph Turian
    Replies:
    5
    Views:
    573
  5. Dave

    FindControl from C# Class

    Dave, Feb 3, 2010, in forum: ASP .Net
    Replies:
    0
    Views:
    763
Loading...

Share This Page