Use Request

  • Thread starter Thread starter rn5a
  • Start date Start date
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?
 
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...
 
Back
Top