request is not available in this context

R

ruthiefy

Hi,

I created a class(AAA) which derives from System.Web.UI.Page.
In the new class (AAA) I created some private variables and I
initialize the variables from the property "Request".
For some reason, I get an exception in the initialization -
"request is not available in this context" .
I tried the same with the property "Session" and didn't have any
problems.

Mayby someone has any idea ??

Thanks.
Ruthie.
 
G

Guest

Good morning,

I'm quessing, you are initializing variables like this:

public class MyPageClass : System.Web.UI.Page
{
private string myVariable = Request.QueryString["blabla"];
....
}
which is not allowed because initialization takes place before HttpRequest
instance is created. If you use any of the page core objects like Session,
Request you can initialize variables (starting from) Init event:

public class MyPageClass : System.Web.UI.Page
{
private string myVariable;

protected void Page_Init(object sender, EventArgs e)
{
this.myVariable = Request.QueryString["blabla"];
}
}

or use 'lazy' initialization in conjunction with a property:

public class MyPageClass : System.Web.UI.Page
{
private string myVariable;
protected /* or public */ string MyVariable
{
get
{
if (this.myVariable == null)
{
this.myVariable = Request.QueryString["blabla"];
if (this.myVariable == null)
this.myVariable = String.Empty;
}
return this.myVariable;
}
}

}

Then use the property instead of private variable:

MyExtraFunction(MyVariable);


Hope this helps
 
R

ruthiefy

Good morning,

I'm quessing, you are initializing variables like this:

public class MyPageClass : System.Web.UI.Page
{
private string myVariable = Request.QueryString["blabla"];
...}

which is not allowed because initialization takes place before HttpRequest
instance is created. If you use any of the page core objects like Session,
Request you can initialize variables (starting from) Init event:

public class MyPageClass : System.Web.UI.Page
{
private string myVariable;

protected void Page_Init(object sender, EventArgs e)
{
this.myVariable = Request.QueryString["blabla"];
}

}

or use 'lazy' initialization in conjunction with a property:

public class MyPageClass : System.Web.UI.Page
{
private string myVariable;
protected /* or public */ string MyVariable
{
get
{
if (this.myVariable == null)
{
this.myVariable = Request.QueryString["blabla"];
if (this.myVariable == null)
this.myVariable = String.Empty;
}
return this.myVariable;
}
}

}

Then use the property instead of private variable:

MyExtraFunction(MyVariable);

Hope this helps
--
Milosz



I created a class(AAA) which derives from System.Web.UI.Page.
In the new class (AAA) I created some private variables and I
initialize the variables from the property "Request".
For some reason, I get an exception in the initialization -
"request is not available in this context" .
I tried the same with the property "Session" and didn't have any
problems.
Mayby someone has any idea ??
Thanks.
Ruthie.- Hide quoted text -

- Show quoted text -

Hi Milosz,

Thanks so much for your excellent detailed explanation!!!
Problem is solved.

Best Regards,
Ruthie.
 

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,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top