By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,938 Members | 2,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,938 IT Pros & Developers. It's quick & easy.

How to access a page level variable from within a class instance

P: n/a
Is there a way for an instance of a custom class to access an ASPX page
level variable? I know that I can access a Session variable from
within a class using the following code:

myClassVar =
System.Web.HttpContext.Current.Session.Item("mySes sionVar")

But I can't figure out if it is possible to access a page level
variable from within a class.

For example, let's say I had the following code for my ASPX page:

Public Class myPage
Inherits System.Web.UI.Page

Public myPageVar as String

Private Sub Page_Load (ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myPageVar = "My Test"
Dim myObject as New myClass()

End Sub
End Class

And that I had the following code for a custom class:

Public Class myClass

Private _localVar as String

Public Sub New()
MyBase.New()
_localVar = ????????? (How do I access the variable myPageVar on
the ASPX page?)
End Sub

End Class

Any help would be greatly appreciated.

Thanks,
Corey

May 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
you have a couple options.

1) pass the variable on the constructor
2) have the aspx page implement an interface, and pass the interface to
class.
3) pass the page instance to the class and use reflection

-- bruce (sqlwork.com)

"Corey B" <co***********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Is there a way for an instance of a custom class to access an ASPX page
level variable? I know that I can access a Session variable from
within a class using the following code:

myClassVar =
System.Web.HttpContext.Current.Session.Item("mySes sionVar")

But I can't figure out if it is possible to access a page level
variable from within a class.

For example, let's say I had the following code for my ASPX page:

Public Class myPage
Inherits System.Web.UI.Page

Public myPageVar as String

Private Sub Page_Load (ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myPageVar = "My Test"
Dim myObject as New myClass()

End Sub
End Class

And that I had the following code for a custom class:

Public Class myClass

Private _localVar as String

Public Sub New()
MyBase.New()
_localVar = ????????? (How do I access the variable myPageVar on
the ASPX page?)
End Sub

End Class

Any help would be greatly appreciated.

Thanks,
Corey

May 23 '06 #2

P: n/a
Yeah I was trying to avoid passing anything to the class (either in the
constructor or otherwise). I am implementing my own XML serialization
for the class and the serialization engine seems to need a
parameterless constructor for the serialization to work. I just wasn't
sure if there was some built in way that a class could reference a
variable on the page where the class was instantiated.

Corey

May 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.