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

Web User Control and using session, request and response

P: n/a
Hi there,

I am busy writing a new asp.net application and I am reusing some of my
existing asp functions and methods in a user control. I need access to
session, request and response in some of the functions and I can't find out
how to do it. Here is an example of what I do and I get the following

Cannot refer to an instance member of a class from within a shared method or
shared member initializer without an explicit instance of the class

on this line
client = Request.ServerVariables("HTTP_USER_AGENT")
Public Shared Function GetIEVersion() As Double
Dim IEVer, client, ver
client = Request.ServerVariables("HTTP_USER_AGENT")

If InStr(1, client, "MSIE", 1) > 0 Then
IEVer = Split(client, ";", -1, 1)
ver = Split(Trim(IEVer(1)), " ")(1)
Else
ver = -9999
End If
Return ver
End Function

if I can find out how to include files like in classic ASP it would also do
it for me.
#include file = "inc\utils.asp" in asp = ? in asp.net

Regards,
George
Nov 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Hi George:

Use HttpContext.Current, which will give you access to the curent
request context with Request, Response, and Session properties.

--
Scott
http://www.OdeToCode.com/

On Thu, 30 Sep 2004 11:14:06 +0200, "George G."
<george.g@--xx--buildsmart.co.za> wrote:
Hi there,

I am busy writing a new asp.net application and I am reusing some of my
existing asp functions and methods in a user control. I need access to
session, request and response in some of the functions and I can't find out
how to do it. Here is an example of what I do and I get the following

Cannot refer to an instance member of a class from within a shared method or
shared member initializer without an explicit instance of the class

on this line
client = Request.ServerVariables("HTTP_USER_AGENT")
Public Shared Function GetIEVersion() As Double
Dim IEVer, client, ver
client = Request.ServerVariables("HTTP_USER_AGENT")

If InStr(1, client, "MSIE", 1) > 0 Then
IEVer = Split(client, ";", -1, 1)
ver = Split(Trim(IEVer(1)), " ")(1)
Else
ver = -9999
End If
Return ver
End Function

if I can find out how to include files like in classic ASP it would also do
it for me.
#include file = "inc\utils.asp" in asp = ? in asp.net

Regards,
George


Nov 18 '05 #2

P: n/a
Thanks for your answers. Accessing page from user controls seemed be
simple, I should use class browser more.

But, I still need more help.

I tried to access the page from a user control, doing it this way:
Protected UC1 As UC1 = DirectCast(Page, Parent_Page)
Parent_Page.Label1.Text = "Text here" ' error here

"Label1" is declared as Public in "Parent_Page".

This gives me error "Object reference not set to an instance of an
object." at the runtime.

Then I tried to set "Label1" as "Public Shared", now (it works, but) vbc
gives me following warning when compiling:
"warning BC42025: Access of shared member through an instance;
qualifying expression will not be evaluated."

Isn't here conflict with error and warning messages? Error message says
that I don't use instance of an object, but warning message says I use
an instance.

I don't understand why I would need to use another instance of the
pageīs user control (or declaring class shared) if user controls of the
page are already declared in the page, and I have a reference to the
page from a user control.

What I miss here?
Nov 18 '05 #3

P: n/a
Sorry, I maybe a little tired.

Previous post was meaned to be continue to the thread "How to access: a
page from a User control, and another User control from another one?"
Nov 18 '05 #4

P: n/a
> I tried to access the page from a user control, doing it this way:
Protected UC1 As UC1 = DirectCast(Page, Parent_Page)
You are stuffing a Page class into a usercontrol? (Inheritence my allow
this, but ...)
Parent_Page.Label1.Text = "Text here" ' error here
This gives me error "Object reference not set to an instance of an
object." at the runtime.
Here you are using the class Parent_Page and not an instance of that class.

How about something like:

Dim MyParent as Parent_Page = DirectCast(Page, Parent_Page)
MyParent.Label1.Text = "Text here"

(untested)

Greg
"qwerty" <x@y.zzz> wrote in message
news:41***********************@news.song.fi... Thanks for your answers. Accessing page from user controls seemed be
simple, I should use class browser more.

But, I still need more help.

I tried to access the page from a user control, doing it this way:
Protected UC1 As UC1 = DirectCast(Page, Parent_Page)
Parent_Page.Label1.Text = "Text here" ' error here

"Label1" is declared as Public in "Parent_Page".

This gives me error "Object reference not set to an instance of an
object." at the runtime.

Then I tried to set "Label1" as "Public Shared", now (it works, but) vbc
gives me following warning when compiling:
"warning BC42025: Access of shared member through an instance; qualifying
expression will not be evaluated."

Isn't here conflict with error and warning messages? Error message says
that I don't use instance of an object, but warning message says I use an
instance.

I don't understand why I would need to use another instance of the pageīs
user control (or declaring class shared) if user controls of the page are
already declared in the page, and I have a reference to the page from a
user control.

What I miss here?

Nov 18 '05 #5

P: n/a
Scott,

Great, it worked, thank you.

George.
Nov 18 '05 #6

P: n/a
Discussion is now mixed in this thread, but good that this threadīs
original problem got already solved.

Greg Burns wrote:
I tried to access the page from a user control, doing it this way:
Protected UC1 As UC1 = DirectCast(Page, Parent_Page)
You are stuffing a Page class into a usercontrol? (Inheritence my allow
this, but ...)

You are right, I typed wrong.
Fix: Protected Parent_Page As Parent_Page = DirectCast(Page, Parent_Page)
Parent_Page.Label1.Text = "Text here" ' error here
This gives me error "Object reference not set to an instance of an
object." at the runtime.

Here you are using the class Parent_Page and not an instance of that class.

How about something like:

Dim MyParent as Parent_Page = DirectCast(Page, Parent_Page)
MyParent.Label1.Text = "Text here"

(untested)

I already tried this and got the error above.

BTW: Does it matter somehow if I declare an object as same name than a
class? I haven't noticed it would cause error (including this problem),
but it may be bad programming habit.. This relates more on VB.Net than
ASP.Net but I think many people can answer this quickly here in this
thread too.
Nov 18 '05 #7

P: n/a
(Sorry about the thread mix up)
Protected Parent_Page As Parent_Page = DirectCast(Page, Parent_Page)
I thought it was odd you were using a Protected modifier instead of Dim, but
I ignored it. Now I am begining to wonder...

You can only use Protected at a class level variable. Are you defining this
there and not in say, Page_Load? Try defining it at class level, but set it
in Page_Load.

Protected myParent_Page As Parent_Page

Sub Page_Load
myParent_Page = DirectCast(Page, Parent_Page)
End Sub

Just a thought. Why protected BTW and not Private?
BTW: Does it matter somehow if I declare an object as same name than a
class?
I don't know that it matters, but it sure can be confusing!

Greg
"qwerty" <x@y.zzz> wrote in message
news:41***********************@news.song.fi... Discussion is now mixed in this thread, but good that this threadīs
original problem got already solved.

Greg Burns wrote:
I tried to access the page from a user control, doing it this way:
Protected UC1 As UC1 = DirectCast(Page, Parent_Page)


You are stuffing a Page class into a usercontrol? (Inheritence my allow
this, but ...)

You are right, I typed wrong.
Fix: Protected Parent_Page As Parent_Page = DirectCast(Page, Parent_Page)
Parent_Page.Label1.Text = "Text here" ' error here
This gives me error "Object reference not set to an instance of an
object." at the runtime.

Here you are using the class Parent_Page and not an instance of that
class.

How about something like:

Dim MyParent as Parent_Page = DirectCast(Page, Parent_Page)
MyParent.Label1.Text = "Text here"

(untested)

I already tried this and got the error above.

BTW: Does it matter somehow if I declare an object as same name than a
class? I haven't noticed it would cause error (including this problem),
but it may be bad programming habit.. This relates more on VB.Net than
ASP.Net but I think many people can answer this quickly here in this
thread too.

Nov 18 '05 #8

P: n/a
Greg Burns wrote:
(Sorry about the thread mix up) You continued, but I started confusing others..
Protected Parent_Page As Parent_Page = DirectCast(Page, Parent_Page)

I thought it was odd you were using a Protected modifier instead of Dim, but
I ignored it. Now I am begining to wonder...

You can only use Protected at a class level variable. Are you defining this
there and not in say, Page_Load? Try defining it at class level, but set it
in Page_Load.

I have been thinking for a while that what "protected" means :) Now the
word itself seems more clear to me. Iīm new to VB.Net also, actually
ASP.Net and VB.Net are totally new to me. Iīve been learning them only
for a few days in the last couple of weeks :)

So your example is functional (Dim is same as Private), but I rejected
it too easily not looking it carefully. Well, itīs good to put also this
fault caused by tiredness (learning too much ASP.Net at nights) :)
Protected myParent_Page As Parent_Page

Sub Page_Load
myParent_Page = DirectCast(Page, Parent_Page)
End Sub

Just a thought. Why protected BTW and not Private?

BTW: Does it matter somehow if I declare an object as same name than a
class?

I don't know that it matters, but it sure can be confusing!

Yes, thatīs so.. Possibly I use some particural prefix in future.

Now I can reference the page from a User Control.
But, still I can't use another User Control from another one, becouse I
use Master Pages. I tested that without them there wouldn't be this
problem (and tested also creating Master Page with my user controls from
clean). But there has to be solution for this also.

The thing what I would like to do is: call Master Pageīs User Control
from aspx-Pageīs User Control. Now with way than above I get the same
error: "Object reference not set to an instance of an object.". Looks
like that the user control doesn't found there. Maybe itīs somewhat
higher level in the Master Page. Looks like Parent don't work neither.
Ideas?
Nov 18 '05 #9

P: n/a
Protected Parent_Page As Parent_Page = DirectCast(Page, Parent_Page)

I thought it was odd you were using a Protected modifier instead of Dim,
but I ignored it. Now I am begining towonder...

You can only use Protected at a class level variable. Are you defining
this there and not in say, Page_Load? Try defining it at class level,
but set it in Page_Load.

I have been thinking for a while that what "protected" means :) Now the
word itself seems more clear to me. Iīm new to VB.Net also, actually
ASP.Net and VB.Net are totally new to me. Iīve been learning them only for
a few days in the last couple of weeks :)

So your example is functional (Dim is same as Private), but I rejected it
too easily not looking it carefully. Well, itīs good to put also this
fault caused by tiredness (learning too much ASP.Net at nights) :)


Yes Dim is equivalent to Private at the class level (but who does that?),
BUT not inside a procedure. Private/Protected keywords are not valid inside
a procedure (you'll see "not valid on a local variable declaration"). That
is what I was commenting on. You do not have a valid reference to your
Parent_Page object at the class level when you do: (I am sure it is due to
the order of events that the page/uc is built)

Protected Parent_Page As Parent_Page = DirectCast(Page, Parent_Page)

I was suggesting you move the DirectCase inside Page_Load so that you can be
assured "it is ready".

I just did I simple test and confirmed my supsicion. I added this code to a
usercontrol:

Private x as Page=DirectCase(Page, WebForm1)

Sub Page_Load
Response.write(x.ispostback) ' <-- blows up with object reference not
set to an instance of an object
End Sub

Simple changing the code to this works:

Private x As Page

Sub Page_Load
x =DirectCase(Page, WebForm1)
Response.write(x.ispostback)
End Sub
I have not used "Master Pages". I assume you are not using ASPNET 2? So
when you refer to them, are you referring to a methodology, or are you using
this:
http://www.asp.net/ControlGallery/Co...385&tabindex=2
The thing what I would like to do is: call Master Pageīs User Control from
aspx-Pageīs User Control.


What exactly to you mean by call as UserControl? Do you want to read a
property from it? Or execute a method on it?

One possible way to get your usercontrols to "talk" with each other is to
have them raise custom events, that you hosting page could handle and direct
the communication.

Greg

Nov 18 '05 #10

P: n/a
Greg Burns wrote:
Protected Parent_Page As Parent_Page = DirectCast(Page, Parent_Page)
I thought it was odd you were using a Protected modifier instead of Dim,
but I ignored it. Now I am begining towonder...

You can only use Protected at a class level variable. Are you defining
this there and not in say, Page_Load? Try defining it at class level,
but set it in Page_Load.
I have been thinking for a while that what "protected" means :) Now the
word itself seems more clear to me. Iīm new to VB.Net also, actually
ASP.Net and VB.Net are totally new to me. Iīve been learning them only for
a few days in the last couple of weeks :)

So your example is functional (Dim is same as Private), but I rejected it
too easily not looking it carefully. Well, itīs good to put also this
fault caused by tiredness (learning too much ASP.Net at nights) :)

Yes Dim is equivalent to Private at the class level (but who does that?),
BUT not inside a procedure. Private/Protected keywords are not valid inside
a procedure (you'll see "not valid on a local variable declaration"). That
is what I was commenting on. You do not have a valid reference to your
Parent_Page object at the class level when you do: (I am sure it is due to
the order of events that the page/uc is built)

I noticed that later. Thanks for specifying. Protected Parent_Page As Parent_Page = DirectCast(Page, Parent_Page)

I was suggesting you move the DirectCase inside Page_Load so that you can be
assured "it is ready". I noticed that and changed my code to work way, but forget to mention
about it earlier.
I just did I simple test and confirmed my supsicion. I added this code to a
usercontrol:

Private x as Page=DirectCase(Page, WebForm1)

Sub Page_Load
Response.write(x.ispostback) ' <-- blows up with object reference not
set to an instance of an object
End Sub

Simple changing the code to this works:

Private x As Page

Sub Page_Load
x =DirectCase(Page, WebForm1)
Response.write(x.ispostback)
End Sub I had declared my other User Control, to where I wanted to have access,
later in the code, so this helped to this problem.

With your advice I can now access Pagesīs another User Control from
another one. So there is no problem anymore.
I have not used "Master Pages". I assume you are not using ASPNET 2? So
when you refer to them, are you referring to a methodology, or are you using
this:
http://www.asp.net/ControlGallery/Co...385&tabindex=2 I use Asp.Net 1.1 and that ASP.NET Teamīs Master Pages -control what you
are referring to.
The thing what I would like to do is: call Master Pageīs User Control from
aspx-Pageīs User Control.

What exactly to you mean by call as UserControl? Do you want to read a
property from it? Or execute a method on it?

At first I would like to see it, have a reference or instance of it
somehow, so I could access itīs propertys, controls, functions and
varibles. It doesn't matter, I can do it in many ways.

The problem is now this: I declare User Controls in Master Page, which
are included in the Page. Page has also User Controlīs which has to
access Master Pageīs User Controls. But I can't do this, I have tried to
access Master Pageīs User Controls several ways, but they doesn't found,
I can't see them.
One possible way to get your usercontrols to "talk" with each other is to
have them raise custom events, that you hosting page could handle and direct
the communication.

That would one possibility, I could hide all above prosessing behind my
own events and this would be in harmony with object oriented programming
reasoning.

But now, after designing the application for a long time, I would like
to do it by calling User Controls directly, at least at first.

BTW, In my mind came, that when using events for this problem, wouldn't
I need a reference to a User Control like I need it now, when accessing
straight to them?
Nov 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.