Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. How should I create a class that I can access across multiple ASP.NET pages?

How should I create a class that I can access across multiple ASP.NET pages?

Scheduled Pinned Locked Moved ASP.NET
csharpquestionasp-netdotnetsysadmin
8 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adam Brown 3
    wrote on last edited by
    #1

    I want to create a class to persist some data which will be accessible no matter what page I navigate to. I am using Server.Transfer(path, true) to switch between pages, and my first thought was to use a static class. But a static class can only contain static members and I need to instantiate new objects on different pages that need to be accessible from subsequent pages. What is the best way to persist this data in a class? Language is C#, .Net framework 3.5.

    L S T O 4 Replies Last reply
    0
    • A Adam Brown 3

      I want to create a class to persist some data which will be accessible no matter what page I navigate to. I am using Server.Transfer(path, true) to switch between pages, and my first thought was to use a static class. But a static class can only contain static members and I need to instantiate new objects on different pages that need to be accessible from subsequent pages. What is the best way to persist this data in a class? Language is C#, .Net framework 3.5.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It is very simple. Just create any class in your web application in .cs file or refer any dll (contains your class) create instance of it. Store the object in session or application and you can access it from any of your subsequent .aspx pages. Hope this will help!

      Jinal Desai - LIVE Experience is mother of sage....

      1 Reply Last reply
      0
      • A Adam Brown 3

        I want to create a class to persist some data which will be accessible no matter what page I navigate to. I am using Server.Transfer(path, true) to switch between pages, and my first thought was to use a static class. But a static class can only contain static members and I need to instantiate new objects on different pages that need to be accessible from subsequent pages. What is the best way to persist this data in a class? Language is C#, .Net framework 3.5.

        S Offline
        S Offline
        Sandesh M Patil
        wrote on last edited by
        #3

        Naptown Brown wrote:

        But a static class can only contain static members and I need to instantiate new objects on different pages that need to be accessible from subsequent pages.

        you can use static class by using classname.methodname for this you have to inherit class at the top of the page.aspx.cs using namespacename.classname; hope this may help u............. :)

        1 Reply Last reply
        0
        • A Adam Brown 3

          I want to create a class to persist some data which will be accessible no matter what page I navigate to. I am using Server.Transfer(path, true) to switch between pages, and my first thought was to use a static class. But a static class can only contain static members and I need to instantiate new objects on different pages that need to be accessible from subsequent pages. What is the best way to persist this data in a class? Language is C#, .Net framework 3.5.

          O Offline
          O Offline
          overtech06
          wrote on last edited by
          #4

          Create a class that inherits "System.UI.Web.Page" and put the properties there that you need to be able to access from all pages. Be sure to build your accessors there as well. All other pages in your web application should inherit from that class. Then the properties you built there will be accessible... Something like this:

          // Page class - myPage.cs
          Public class myPage : Page
          {
          protected SqlConnection conn;
          protected SqlCommand cmd;

          public myPage() : base()
          {
              string connectionString = ConfigurationManager.ConnectionString\["DBConnectionString"\].ConnectionString;
              this.conn = new SqlConnection(connectionString);
              this.cmd = new SqlCommand();
              this.cmd.Connection = conn;
          }
          
          protected int myIntProperty
          {
              // From here you can call your static classes
              get { return CodeLayer.myProperties.myInt; }
              set { CodeLayer.myProperties.myInt = value; }
          
          }
          

          }

          Now when you create a new page in your web application, your page should inherit this class. In this example that would mean that whenever the page loads, you would already have an SqlConnection and SqlCommand object defined and usable and you can access your "myInt" property.

          public partial class PageOne : myPage
          {
          protected void Page_Load(object sender, EventArgs e)
          {
          // the SqlConnection and SqlCommand objects are already created
          cmd.CommandText = "SELECT * FROM myTable";
          this.myIntProperty = 10;
          }
          }

          - Dave

          A 1 Reply Last reply
          0
          • A Adam Brown 3

            I want to create a class to persist some data which will be accessible no matter what page I navigate to. I am using Server.Transfer(path, true) to switch between pages, and my first thought was to use a static class. But a static class can only contain static members and I need to instantiate new objects on different pages that need to be accessible from subsequent pages. What is the best way to persist this data in a class? Language is C#, .Net framework 3.5.

            T Offline
            T Offline
            T M Gray
            wrote on last edited by
            #5

            Why are you using Server.Transfer? Do you really need all of the viewstate and posted data from one page to be sent to the other? That can be pretty inefficient. Create a class file in your web project. I would probably put it in its own namespace and then for each page that needs to use the class, add a using statement for that namespace. What other objects do you need to instantiate and where do those classes live? What kind of persistence are you thinking of? Persisting in memory is usually not a good idea as that doesn't scale very well. You might want to do some research on some design patterns like the domain object model. Then you can look for an ORM (Object relational mapper) that will help you get your objects into and out of a database.

            A 1 Reply Last reply
            0
            • O overtech06

              Create a class that inherits "System.UI.Web.Page" and put the properties there that you need to be able to access from all pages. Be sure to build your accessors there as well. All other pages in your web application should inherit from that class. Then the properties you built there will be accessible... Something like this:

              // Page class - myPage.cs
              Public class myPage : Page
              {
              protected SqlConnection conn;
              protected SqlCommand cmd;

              public myPage() : base()
              {
                  string connectionString = ConfigurationManager.ConnectionString\["DBConnectionString"\].ConnectionString;
                  this.conn = new SqlConnection(connectionString);
                  this.cmd = new SqlCommand();
                  this.cmd.Connection = conn;
              }
              
              protected int myIntProperty
              {
                  // From here you can call your static classes
                  get { return CodeLayer.myProperties.myInt; }
                  set { CodeLayer.myProperties.myInt = value; }
              
              }
              

              }

              Now when you create a new page in your web application, your page should inherit this class. In this example that would mean that whenever the page loads, you would already have an SqlConnection and SqlCommand object defined and usable and you can access your "myInt" property.

              public partial class PageOne : myPage
              {
              protected void Page_Load(object sender, EventArgs e)
              {
              // the SqlConnection and SqlCommand objects are already created
              cmd.CommandText = "SELECT * FROM myTable";
              this.myIntProperty = 10;
              }
              }

              - Dave

              A Offline
              A Offline
              Adam Brown 3
              wrote on last edited by
              #6

              Thanks so much for the great example, Dave. I did not consider something like this. It looks like a very workable solution for calling static methods, but my real goal is to persist data in objects between calls to different pages and to do that I cannot use static classes since I have to instantiate data. I thought about it alot and I think the best approach is to serialize the data in an XML data island between page calls. Then I can pick it up on the next page, de-serialize it, and move on. Thanks again. Adam

              O 1 Reply Last reply
              0
              • T T M Gray

                Why are you using Server.Transfer? Do you really need all of the viewstate and posted data from one page to be sent to the other? That can be pretty inefficient. Create a class file in your web project. I would probably put it in its own namespace and then for each page that needs to use the class, add a using statement for that namespace. What other objects do you need to instantiate and where do those classes live? What kind of persistence are you thinking of? Persisting in memory is usually not a good idea as that doesn't scale very well. You might want to do some research on some design patterns like the domain object model. Then you can look for an ORM (Object relational mapper) that will help you get your objects into and out of a database.

                A Offline
                A Offline
                Adam Brown 3
                wrote on last edited by
                #7

                I put a class file in my project and put it in its own namespace and it is accessible to every page, but I really need access to the data in objects that I instantiate. The data I need must persist for the life of the applicaiton. It is kind of like a wizard app where I am accumulating data page to page for final submission at the end. This is only a 3 month project so I want to keep it simple since the timelines are a bit tight. I thought about it alot and I think the best approach is to serialize the data in an XML data island between page transfers. Then I can pick it up on the next page, de-serialize it, and move on. Thanks for your help. Adam

                1 Reply Last reply
                0
                • A Adam Brown 3

                  Thanks so much for the great example, Dave. I did not consider something like this. It looks like a very workable solution for calling static methods, but my real goal is to persist data in objects between calls to different pages and to do that I cannot use static classes since I have to instantiate data. I thought about it alot and I think the best approach is to serialize the data in an XML data island between page calls. Then I can pick it up on the next page, de-serialize it, and move on. Thanks again. Adam

                  O Offline
                  O Offline
                  overtech06
                  wrote on last edited by
                  #8

                  You are correct, the data must be stored somewhere. I generally use a database, but any storage method would work. The idea in my example would be that CodeLayer.myProperties.myInt; would actually do the XML de-serialization in the property's get accessor. It just makes accessing those values very simple when you get to the page level... - Dave

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups