Public Functions in Global.asax?

Discussion in 'ASP .Net' started by Mat, Nov 3, 2004.

  1. Mat

    Mat Guest

    Hi,

    I am trying to create a set of global functions that are available to
    any page. However I am not really getting anywhere, even after looking
    at several examples.

    This is the code I have so far:

    (simple.aspx)
    <%@ Page Language="vb" AutoEventWireup="false"
    Inherits="RealSimple.CodeBehindSimple" src="simple.vb" %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
    "http://www.w3.org/TR/REC-html40/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1">
    <title></title>
    </head>

    <body>

    <form runat="server">

    <asp:Label id="moog" runat="server" />
    <asp:Button id="btn_doit" runat="server" OnClick="buttonclick" />

    </form>

    </body>
    </html>



    (simple.vb)
    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.HtmlControls

    Namespace RealSimple
    Public Class CodeBehindSimple
    Inherits Page

    Protected WithEvents moog As label

    Public Sub New()
    End Sub


    sub buttonclick(s as object, e as eventargs)
    moog.text = myGlobal.SayGoodbye("M")
    end sub
    End Class
    End Namespace



    (global.asax)
    <%@ Application language="VB" Classname="myGlobal" %>
    <script runat="server">
    Sub Application_Start(Sender As Object, E As EventArgs)
    ' Code that runs on application startup
    End Sub


    Sub Application_End(Sender As Object, E As EventArgs)
    ' Code that runs on application shutdown
    End Sub


    Sub Application_Error(Sender As Object, E As EventArgs)
    ' Code that runs when an unhandled error occurs
    End Sub


    Sub Session_Start(Sender As Object, E As EventArgs)
    ' Code that runs when a new session is started
    End Sub


    Sub Session_End(Sender As Object, E As EventArgs)
    ' Code that runs when a session ends
    End Sub


    Public Shared Function SayGoodbye( name As String ) As String
    Return name
    End Function

    </script>


    The problem is, when try and fire this off (simple.aspx) I get the
    following error:


    BC30451: Name 'myGlobal' is not declared.


    Any ideas what is going wrong?

    I have looked at 'http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=708199'
    which is what I have based my code on.

    I am not using VS, just a simple text editor.

    Many thanks,
    Mat.
     
    Mat, Nov 3, 2004
    #1
    1. Advertising

  2. Create a Class Library, and use them out of the Class Library.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    I get paid good money to
    solve puzzles for a living

    "Mat" <> wrote in message
    news:...
    > Hi,
    >
    > I am trying to create a set of global functions that are available to
    > any page. However I am not really getting anywhere, even after looking
    > at several examples.
    >
    > This is the code I have so far:
    >
    > (simple.aspx)
    > <%@ Page Language="vb" AutoEventWireup="false"
    > Inherits="RealSimple.CodeBehindSimple" src="simple.vb" %>
    >
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
    > "http://www.w3.org/TR/REC-html40/strict.dtd">
    > <html>
    > <head>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=iso-8859-1">
    > <title></title>
    > </head>
    >
    > <body>
    >
    > <form runat="server">
    >
    > <asp:Label id="moog" runat="server" />
    > <asp:Button id="btn_doit" runat="server" OnClick="buttonclick" />
    >
    > </form>
    >
    > </body>
    > </html>
    >
    >
    >
    > (simple.vb)
    > Imports System
    > Imports System.Web.UI
    > Imports System.Web.UI.WebControls
    > Imports System.Web.UI.HtmlControls
    >
    > Namespace RealSimple
    > Public Class CodeBehindSimple
    > Inherits Page
    >
    > Protected WithEvents moog As label
    >
    > Public Sub New()
    > End Sub
    >
    >
    > sub buttonclick(s as object, e as eventargs)
    > moog.text = myGlobal.SayGoodbye("M")
    > end sub
    > End Class
    > End Namespace
    >
    >
    >
    > (global.asax)
    > <%@ Application language="VB" Classname="myGlobal" %>
    > <script runat="server">
    > Sub Application_Start(Sender As Object, E As EventArgs)
    > ' Code that runs on application startup
    > End Sub
    >
    >
    > Sub Application_End(Sender As Object, E As EventArgs)
    > ' Code that runs on application shutdown
    > End Sub
    >
    >
    > Sub Application_Error(Sender As Object, E As EventArgs)
    > ' Code that runs when an unhandled error occurs
    > End Sub
    >
    >
    > Sub Session_Start(Sender As Object, E As EventArgs)
    > ' Code that runs when a new session is started
    > End Sub
    >
    >
    > Sub Session_End(Sender As Object, E As EventArgs)
    > ' Code that runs when a session ends
    > End Sub
    >
    >
    > Public Shared Function SayGoodbye( name As String ) As String
    > Return name
    > End Function
    >
    > </script>
    >
    >
    > The problem is, when try and fire this off (simple.aspx) I get the
    > following error:
    >
    >
    > BC30451: Name 'myGlobal' is not declared.
    >
    >
    > Any ideas what is going wrong?
    >
    > I have looked at

    'http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=708199'
    > which is what I have based my code on.
    >
    > I am not using VS, just a simple text editor.
    >
    > Many thanks,
    > Mat.
     
    Kevin Spencer, Nov 3, 2004
    #2
    1. Advertising

  3. Mat

    Marina Guest

    I think that is because your class is in one namespace, and myGlobal ends up
    being in another.

    In any case, I don't recommend you put any function in global other then the
    ones it itself will need. Create a brand new class, and put your shared
    functions in there.

    "Mat" <> wrote in message
    news:...
    > Hi,
    >
    > I am trying to create a set of global functions that are available to
    > any page. However I am not really getting anywhere, even after looking
    > at several examples.
    >
    > This is the code I have so far:
    >
    > (simple.aspx)
    > <%@ Page Language="vb" AutoEventWireup="false"
    > Inherits="RealSimple.CodeBehindSimple" src="simple.vb" %>
    >
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
    > "http://www.w3.org/TR/REC-html40/strict.dtd">
    > <html>
    > <head>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=iso-8859-1">
    > <title></title>
    > </head>
    >
    > <body>
    >
    > <form runat="server">
    >
    > <asp:Label id="moog" runat="server" />
    > <asp:Button id="btn_doit" runat="server" OnClick="buttonclick" />
    >
    > </form>
    >
    > </body>
    > </html>
    >
    >
    >
    > (simple.vb)
    > Imports System
    > Imports System.Web.UI
    > Imports System.Web.UI.WebControls
    > Imports System.Web.UI.HtmlControls
    >
    > Namespace RealSimple
    > Public Class CodeBehindSimple
    > Inherits Page
    >
    > Protected WithEvents moog As label
    >
    > Public Sub New()
    > End Sub
    >
    >
    > sub buttonclick(s as object, e as eventargs)
    > moog.text = myGlobal.SayGoodbye("M")
    > end sub
    > End Class
    > End Namespace
    >
    >
    >
    > (global.asax)
    > <%@ Application language="VB" Classname="myGlobal" %>
    > <script runat="server">
    > Sub Application_Start(Sender As Object, E As EventArgs)
    > ' Code that runs on application startup
    > End Sub
    >
    >
    > Sub Application_End(Sender As Object, E As EventArgs)
    > ' Code that runs on application shutdown
    > End Sub
    >
    >
    > Sub Application_Error(Sender As Object, E As EventArgs)
    > ' Code that runs when an unhandled error occurs
    > End Sub
    >
    >
    > Sub Session_Start(Sender As Object, E As EventArgs)
    > ' Code that runs when a new session is started
    > End Sub
    >
    >
    > Sub Session_End(Sender As Object, E As EventArgs)
    > ' Code that runs when a session ends
    > End Sub
    >
    >
    > Public Shared Function SayGoodbye( name As String ) As String
    > Return name
    > End Function
    >
    > </script>
    >
    >
    > The problem is, when try and fire this off (simple.aspx) I get the
    > following error:
    >
    >
    > BC30451: Name 'myGlobal' is not declared.
    >
    >
    > Any ideas what is going wrong?
    >
    > I have looked at

    'http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=708199'
    > which is what I have based my code on.
    >
    > I am not using VS, just a simple text editor.
    >
    > Many thanks,
    > Mat.
     
    Marina, Nov 3, 2004
    #3
  4. Mat

    Mat Guest

    Cheers, I took your advice and wrote a class. Works like a charm.

    Thanks!!!
    Mat.

    "Marina" <> wrote in message news:<#>...
    > I think that is because your class is in one namespace, and myGlobal ends up
    > being in another.
    >
    > In any case, I don't recommend you put any function in global other then the
    > ones it itself will need. Create a brand new class, and put your shared
    > functions in there.
    >
    > "Mat" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I am trying to create a set of global functions that are available to
    > > any page. However I am not really getting anywhere, even after looking
    > > at several examples.
    > >
    > > This is the code I have so far:
    > >
    > > (simple.aspx)
    > > <%@ Page Language="vb" AutoEventWireup="false"
    > > Inherits="RealSimple.CodeBehindSimple" src="simple.vb" %>
    > >
    > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
    > > "http://www.w3.org/TR/REC-html40/strict.dtd">
    > > <html>
    > > <head>
    > > <meta http-equiv="Content-Type" content="text/html;
    > > charset=iso-8859-1">
    > > <title></title>
    > > </head>
    > >
    > > <body>
    > >
    > > <form runat="server">
    > >
    > > <asp:Label id="moog" runat="server" />
    > > <asp:Button id="btn_doit" runat="server" OnClick="buttonclick" />
    > >
    > > </form>
    > >
    > > </body>
    > > </html>
    > >
    > >
    > >
    > > (simple.vb)
    > > Imports System
    > > Imports System.Web.UI
    > > Imports System.Web.UI.WebControls
    > > Imports System.Web.UI.HtmlControls
    > >
    > > Namespace RealSimple
    > > Public Class CodeBehindSimple
    > > Inherits Page
    > >
    > > Protected WithEvents moog As label
    > >
    > > Public Sub New()
    > > End Sub
    > >
    > >
    > > sub buttonclick(s as object, e as eventargs)
    > > moog.text = myGlobal.SayGoodbye("M")
    > > end sub
    > > End Class
    > > End Namespace
    > >
    > >
    > >
    > > (global.asax)
    > > <%@ Application language="VB" Classname="myGlobal" %>
    > > <script runat="server">
    > > Sub Application_Start(Sender As Object, E As EventArgs)
    > > ' Code that runs on application startup
    > > End Sub
    > >
    > >
    > > Sub Application_End(Sender As Object, E As EventArgs)
    > > ' Code that runs on application shutdown
    > > End Sub
    > >
    > >
    > > Sub Application_Error(Sender As Object, E As EventArgs)
    > > ' Code that runs when an unhandled error occurs
    > > End Sub
    > >
    > >
    > > Sub Session_Start(Sender As Object, E As EventArgs)
    > > ' Code that runs when a new session is started
    > > End Sub
    > >
    > >
    > > Sub Session_End(Sender As Object, E As EventArgs)
    > > ' Code that runs when a session ends
    > > End Sub
    > >
    > >
    > > Public Shared Function SayGoodbye( name As String ) As String
    > > Return name
    > > End Function
    > >
    > > </script>
    > >
    > >
    > > The problem is, when try and fire this off (simple.aspx) I get the
    > > following error:
    > >
    > >
    > > BC30451: Name 'myGlobal' is not declared.
    > >
    > >
    > > Any ideas what is going wrong?
    > >
    > > I have looked at

    > 'http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=708199'
    > > which is what I have based my code on.
    > >
    > > I am not using VS, just a simple text editor.
    > >
    > > Many thanks,
    > > Mat.
     
    Mat, Nov 4, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    1
    Views:
    1,363
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    576
    SpamProof
    Oct 21, 2003
  3. Ekul
    Replies:
    2
    Views:
    655
  4. =?Utf-8?B?YmNoYXJsZXM=?=

    global.asax and global.asax.cs

    =?Utf-8?B?YmNoYXJsZXM=?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    841
    =?Utf-8?B?YmNoYXJsZXM=?=
    Oct 5, 2004
  5. Mark Rae

    Global.asax / Global.asax.cs in v2

    Mark Rae, May 23, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    3,165
    Mark Rae
    May 23, 2006
Loading...

Share This Page