FindControl in Class

M

MRW

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!
 
R

RHIZOME

Declare new with Public FV_Customer As FormView

e.g. Public FV_Customer As New FormView
 
M

MRW

What the???

Mmm...

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

Thanks for the help!
 
T

Teemu Keiski

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

Teemu Keiski

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
 
M

MRW

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!
 
T

Teemu Keiski

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top