Link to home
Start Free TrialLog in
Avatar of mmedi005
mmedi005

asked on

How do I build a class that uses Response object in ASP .NET?

I want to build a class that uses a sub routine like this......

Imports Microsoft.VisualBasic
Imports System.Web.UI

Public Class security
 
  Protected Sub Is_User_Signed_In(ByVal u)
       
       If u <> "" Then
                 'check user name........
        Else
            Response.Redirect("http://.....")
        End If
    End Sub

End Class

I want this class to be available globally to all my web pages?  How do I do this?  Is there another way, am I taking the wrong approach?
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

You must place your class inside the App_Code and change to:

Public Share Sub Is_User_Signed_In(ByVal u)
       
       If u <> "" Then
                 'check user name........
        Else
            Response.Redirect("http://.....")
        End If
    End Sub

End Class
Avatar of mmedi005
mmedi005

ASKER

Am I importing the correct classes,  and also I have to import the class with whatever webpage I'm using it for, right?
Dear mmedi005,
Yes, you can go with the way you applied now. However, you need to use call System.Web.HttpContext Response or Request object. Check this complete reference regarding mentioned:
http://msdn2.microsoft.com/en-us/library/system.web.httpcontext.aspx
eg:
  If u <> "" Then
                 'check user name........
        Else
            System.Web.HttpContext.Current.Response.Redirect("http://.....")
        End If
In the begining of your webpage (*.vb) you make the import like

Imports MyClass

Then you can use the functions that you have.

You can also do MyClass.MyFuncion bla bla bla
I have placed it inside the App_Code but VS does not like what it sees.  In the code you gave me it has

                       Sub Is_User_Signed_In(ByVal u) - underlined in blue

        If  - same              u <> "" Then

        Else - same
            Response - same .Redirect("http://.....")
        End If - same

    End Sub - same

all underlined in blue...why?
What is U ? Its a string ?

Does you have this sub inside a class

Public Class MyClass

Public Share Sub Is_User_Signed_In(ByVal u As String)
       
       If u <> "" Then
                 'check user name........
        Else
            Response.Redirect("http://.....")
        End If
    End Sub

End Sub

End Class

yes with this on top

Imports Microsoft.VisualBasic
Imports System.Web.UI

Dont know if im importing the correct class

also you have End Sub twice in the bottom, I only have it once...
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Error      1      End of statement expected.      
Error      2      Statement cannot appear outside of a method body.      
Error      3      Statement cannot appear outside of a method body.      
Error      4      Declaration expected.      
Error      5      'End If' must be preceded by a matching 'If'.
Error      6      'End Sub' must be preceded by a matching 'Sub'.      
Error      7      Name 'Is_User_Logged_In' is not declared
Nice, ok, got it down to

Error      1      Name 'Response' is not declared.      
Error      2      Name 'Is_User_Logged_In' is not declared.      

thanks in advance, your helping me a lot and really appreciate it....  :  )
Replace 'Response' with System.Web.HttpContext.Current.Response.Redirect("http://.....").

X_com have already post it before
you the man, thx jpaulino.....

Does that go for all com objects, are they all placed in that class?

thanks again...  :  )