Page.FindControl non-Shared member

T

tshad

I have a .ascx file that I converted to a class.

But I am getting the following error:

error BC30469: Reference to a non-shared member requires an object
reference.

The error is for the Page.FindControl in the following code:

******************************************
Imports System
Imports System.Web
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.SessionState
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.HttpCookie
Imports System.Web.HttpCookieCollection
Imports System.Web.HttpResponse
Imports System.Web.HttpRequest
imports System.Web.HttpContext
Imports System.Web.HttpApplication
Imports System.Web.HttpApplicationState
Imports System.Collections

NameSpace MyFunctions

Public Class PageInit

Public Shared sub PageSetup ()
Dim UserLoggedOn as Label = CType(Page.FindControl("UserLoggedOn"),Label)
Dim UserLoggedOnLabel as Label =
CType(Page.FindControl("UserLoggedOnLabel"),Label)
if not UserLoggedOn is nothing then
if HttpContext.Current.session("LoggedIn") <> nothing then
if HttpContext.Current.session("firstName") <> nothing then
UserLoggedOn.Text = UserLoggedOn.Text &
HttpContext.Current.session("firstName")
if HttpContext.Current.session("lastName") <> nothing then
UserLoggedOn.Text = UserLoggedOn.Text & " " &
HttpContext.Current.session("lastName")
end if
end if
if not UserLoggedOn is nothing then
UserLoggedOn.visible = true
if not UserLoggedOnLabel is nothing then UserLoggedOnLabel.visible =
true
end if
end if
end if
if not HttpContext.Current.session("User") is nothing then _
HttpContext.Current.session("LastPageVisited") =
HttpContext.Current.Session("User").LastPageVisited
End Sub
End Class
End Namespace
*************************************************************************

Why am I getting this error?

Do I need something in front the Page.FindControl?

Thanks,

Tom
 
M

Marina Levit [MVP]

Page is a class. It is not an instance of that class. You should pass in a
variable to your PageSetup routine, which is an instance of the page it
should set up.

In a .ascx, Page is a property of the class, hence you can use it. When you
are just in a function in some other class, it doesn't have such a property.
 
T

tshad

Marina Levit said:
Page is a class. It is not an instance of that class. You should pass in
a variable to your PageSetup routine, which is an instance of the page it
should set up.

In a .ascx, Page is a property of the class, hence you can use it. When
you are just in a function in some other class, it doesn't have such a
property.

Would I then do the following in the function:
**************************************************************
NameSpace MyFunctions

Public Class PageInit

Public Shared sub PageSetup (thePage as Page)
Dim UserLoggedOn as Label =
CType(thePage.FindControl("UserLoggedOn"),Label)
Dim UserLoggedOnLabel as Label =
CType(thePage.FindControl("UserLoggedOnLabel"),Label)
***********************************************************************

And then call it as:

MyFunctions.PageInit.PageSetup(Page)

Thanks,

Tom
 
T

tshad

Marina Levit said:
Page is a class. It is not an instance of that class. You should pass in
a variable to your PageSetup routine, which is an instance of the page it
should set up.

In a .ascx, Page is a property of the class, hence you can use it. When
you are just in a function in some other class, it doesn't have such a
property.

Here is how I set it up and it works great.

****************************************
NameSpace MyFunctions

Public Class PageInit

Public Shared sub PageSetup (thePage as Page)
Dim url as String
Dim urlNoParams as String
Dim UserLoggedOn as Label =
CType(thePage.FindControl("UserLoggedOn"),Label)
Dim UserLoggedOnLabel as Label =
CType(thePage.FindControl("UserLoggedOnLabel"),Label)
if not UserLoggedOn is nothing then
if HttpContext.Current.session("LoggedIn") <> nothing then
if HttpContext.Current.session("firstName") <> nothing then

*******************************************************

I call it like so:

<%@ Import Namespace="MyFunctions" %>

<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
if not IsPostBack
PageInit.PageSetup(Page)

Why do I need to send it the "Page" but not the Session Variables? Isn't
there a direct way to access it as the current Page?

Thanks,

Tom
 

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,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top