Request.Form

R

rn5a

Consider the following 2 code snippets:

-----------------------------------------------
<script runat="server">
Dim strName As String =
System.Web.HttpContext.Current.Request.Form("txtName")

Sub Page_Load(......)
.................
.................
End Sub

'some more Subs here
</script>
-----------------------------------------------


-----------------------------------------------
<script runat="server">
Dim strName As String = Request.Form("txtName")

Sub Page_Load(......)
.................
.................
End Sub

'some more Subs here
</script>
-----------------------------------------------

Note that in both the code snippets, the variable strName has been
declared & assigned a value outside any sub-routine.

The first code snippet works fine but the second code snippet
generates the following error:

**********************
Request is not available in this context.
**********************

pointing to the Dim strName... line. Can someone please explain me why
does the second code snippet generate the above error & why does the
first code snippet work fine?

Thanks.........
 
B

Ben

Dim strName As String = Request.Form("txtName")


This statement will be compiled to execute in the class constructor. At
contruction time the Page class (the aspx page is conpiled into a class
deriving from Page) property 'Request' is not set yet, but is still
null. So accessing by calling Request.Form("txtName") will throw an
exception.

The Page.Request property is only set after the contructor has run (in
AsyncPageBeginProcessRequest).
System.Web.HttpContext.Current.Request.Form("txtName")

This just uses a static property of a differnt class, so it's available
in the Page constructor.

hth

Ben
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top