Web User Control and using session, request and response

G

George G.

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
 
S

Scott Allen

Hi George:

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

qwerty

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?
 
Q

qwerty

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?"
 
G

Greg Burns

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
 
Q

qwerty

Discussion is now mixed in this thread, but good that this thread´s
original problem got already solved.

Greg said:
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)
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.
 
G

Greg Burns

(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
 
Q

qwerty

Greg said:
(Sorry about the thread mix up)
You continued, but I started confusing others..
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?




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?
 
G

Greg Burns

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/ControlDetail.aspx?control=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
 
Q

qwerty

Greg said:
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/ControlDetail.aspx?control=385&tabindex=2
I use Asp.Net 1.1 and that ASP.NET Team´s Master Pages -control what you
are referring to.
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?
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top