views:

181

answers:

5

I think this is a pretty easy question...How do I make a asp.net function global? e.g. If I have a function GetUserInfo() defined on default.aspx how do I call this function from mypage2.aspx?

+1  A: 

Open up default.aspx and take a look at the class name for that page (it'll probably be _Default). Make sure GetUserInfo() is a public static method and you can then call it from mypage2.aspx like:

_Default.GetUserInfo();

Of course the above approach would get messy very quickly. A vastly better approach would be to add a class file to your project and move the GetUserInfo() method into that file. Implementing something like:

public static class Utilities
{
    public static string GetUserInfo()
    { ... }
}

Would allow you to get the call the method on any page with:

Utilities.GetUserInfo();
Chris Pebble
You'll have to make it static for that to work.
Craig
How does one make static in VB.NET? Is there a disadvantage to doing so?
davemackey
Use the Shared keyword.
womp
+2  A: 

You could...

  • define your GetUserInfo() method in helper/utility class and call it as needed from your pages, or
  • create base page class containing your GetUserInfo() method, and have your aspx pages inherit from it.
Craig
+1  A: 

You can also create a static class in which you can place functions which can be called from any place.

static class MyClass {
   void MyFunction() {
   }
}

MyClass.MyFunction();

Or in VB:

Module MyModule
    Public Function MyFunction() 

    End Function
End Module

MyModule.MyFunction()
Ropstah
Can this be done in VB.NET?
davemackey
Yes, however static classes don't exist in VB.NET. The equivalent is a Module. See the updated anwser.
Ropstah
+9  A: 

another alternative is to make a base page class that all of your pages inherit:

public class BasePage : System.Web.UI.Page
{
     public string GetUserInfo()
     {...}

}

All of the aspx pages that need this method can inherit the BasePage class. Since BasePage inherits from System.Web.UI.Page, they will get access to all of the page methods and properties as well.

derek
+1  A: 

I have all of my "global" stuff in a single class either called cProgram or cApp. Starts off with all of the global properties, and then my common methods.

public class cApp
{

             private readonly static string _Env = 
!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["GenMgrEnv"]) ? 
    System.Configuration.ConfigurationManager.AppSettings["GenMgrEnv"] : "Prod";


     public static string Env
    {
        get
        { return _Env; }
    }


    public static int version
    {
        get { return 3; }
    }


    public static string CurrentUser()
    {
        return (System.Web.HttpContext.Current.Request.Cookies["UID"] != null ? System.Web.HttpContext.Current.Request.Cookies["UID"].Value : "");

    }

    public static Guid UserId
    {
        get
        {
            if (System.Web.HttpContext.Current.Session["UserId"] == null)
            {
                loadUserIdentity();
            }
            return new Guid(System.Web.HttpContext.Current.Session["UserId"].ToString());
        }

    }



    public static Control FindControlRecursively(String id, Control parent)
    {
        if (parent == null)
            return null;

        foreach (Control control in parent.Controls)
        {
            if (control.ID == id)
                return control;

            Control child = FindControlRecursively(id, control);

            if (child != null)
                return child;
        }
        return null;
    }
}
JBrooks