3

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
  • Thanks all for the help! I'm well on my way now. Commented Apr 21, 2010 at 0:33

5 Answers 5

15

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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;
    }
}

Comments

2

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.

Comments

1

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();

1 Comment

How does one make static in VB.NET? Is there a disadvantage to doing so?
1

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()

1 Comment

Yes, however static classes don't exist in VB.NET. The equivalent is a Module. See the updated anwser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.