public functions

R

Random

I need to write a global function that I could use in every page codebehind
and every control. I've created a component (page_base.vb) that contains
the class Page_Base...

Public Class Page_Base
Inherits System.Web.UI.Page

Sub Page_Load(...)
(script that runs on every page)
End Sub

Public Function HostRequested() As String
HostRequested = Request.URL.Host
End Function

End Class

I am able to reference this on every page by substituting the "Inherits
System.Web.UI.Page" in the codebehind with "Inherits Page_Base". I am then
able to add onto the Page_Load procedure in the codebehind for any page
specific functionality. I am also able to access the HostRequested function
from the codebehind

So far so good....

Now I want to extend the accessibility to the HostRequested function to
controls that I am creating. The controls, however, cannot recognize the
HostRequested function. I cannot change the 'Inherits' tag because controls
inherit from the System.Web.UI.UserControl class instead of the
System.Web.UI.Page class.

How can I make my HostRequested function accessible from both page
codebehind and controls?
 
2

23s

A control should be able to refernce it's parent page, which (and here's
where I start making this up) you might be able to Ctype() into a Page_Base
and then access the HostRequested function from the resulting object, which
should be a base_page?

Ctype(Me.Page, page_base).HostRequested()

Or just keep all this business at the application level in global.asax I
guess.
 
R

Random

I just tried this but get "Reference to a non-shared member requires an
object reference."

I am calling the function as "Global.HostRequested()", which is the same
error warning I was getting before when I was calling it as
"Page_Base.HostRequested()". Without the class prefix on the function call
the warning says "Name 'HostRequested' is not declared."
 
R

Random

Congratulations! This one worked!


23s said:
A control should be able to refernce it's parent page, which (and here's
where I start making this up) you might be able to Ctype() into a Page_Base
and then access the HostRequested function from the resulting object, which
should be a base_page?

Ctype(Me.Page, page_base).HostRequested()

Or just keep all this business at the application level in global.asax I
guess.
 
J

John Saunders

Random said:
I need to write a global function that I could use in every page codebehind
and every control. I've created a component (page_base.vb) that contains
the class Page_Base...

Public Class Page_Base
Inherits System.Web.UI.Page

Sub Page_Load(...)
(script that runs on every page)
End Sub

Public Function HostRequested() As String
HostRequested = Request.URL.Host
End Function

End Class

I am able to reference this on every page by substituting the "Inherits
System.Web.UI.Page" in the codebehind with "Inherits Page_Base". I am then
able to add onto the Page_Load procedure in the codebehind for any page
specific functionality. I am also able to access the HostRequested function
from the codebehind

So far so good....

Now I want to extend the accessibility to the HostRequested function to
controls that I am creating. The controls, however, cannot recognize the
HostRequested function. I cannot change the 'Inherits' tag because controls
inherit from the System.Web.UI.UserControl class instead of the
System.Web.UI.Page class.

If you need this method to be accessible both to pages and to controls, then
it does not belong in Page_Base. It should be a Shared method, perhaps in
the Global.asax.vb file. You could then refer to Global.HostRequested().

Note that you'd have to change it as follows:
Public Shared Function HostRequested() As String
HostRequested = HttpContext.Current.Request.URL.Host
End Function
 
R

Random

Keyword missing was "Shared". Thanks.

BTW, why wouldn't I want to put it in Page_Base? Is there a performance
hit, or difference in how things are cached or compiled??
 
J

John Saunders

Random said:
Keyword missing was "Shared". Thanks.

BTW, why wouldn't I want to put it in Page_Base? Is there a performance
hit, or difference in how things are cached or compiled??

Because you need it to be accessed both from controls and pages.
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top