Use Request

R

rn5a

I want to use the Request object within the <script> tag but outside
any subs/functions in an ASPX page...something like this (so that I can
use the variable strUserName in any other subs/functions so as to avoid
re-declaring it & re-assigning a value to it in every sub/function
within this page):

<%@ Import Namespace="System.Web.HttpRequest" %>
<script runat="server">
Public strUserName As String = Request.Cookies("UserName").Value

Sub Page_Load(.......)
lblUserName.Text = strUserName
blah....blah....blah......
End Sub
</script>

But I get the error

Request is not available in this context.

pointing to the first line within the <script> tag.

How do I make use of Request under such s scenario?
 
S

Steven Nagy

Well one solution would be to make it a property in your code behind:

Public Readonly Property strUserName as string
get
return Request.Cookies("UserName").Value
end get
End Property


Otherwise, try :
HttpContext.Current.Request

.... because I am not so sure that the Request you are after is in
scope.
You are referring to the Page.Request property which maybe of type
HttpRequest, but because it is an instance, you need access to the Page
instance. So use HttpContext instead.

There may be an easier way than I mentioned here...
 

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,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top