Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a class with postback

Status
Not open for further replies.

bigfoot

Programmer
Joined
May 4, 1999
Messages
1,779
Location
US
This may sound like a silly question but I can't seem to get it to work.

How do I make a class that I created in ASP.NET persist through pagebacks?

I have tried assigning it to a session variable and it still clears.

Rather then me post my ugly code can someone please post me an example of using a class in ASP.NET?

It could be as simple as myclass with name as a parameter. I'm more interested in how it's instantiated and how you get the data to persist through pageback.

Thank you ahead of time.
 
in the simplest terms, you instaniate the object with each postback.
Code:
public class MyPage : Page
{
  private SomeObject someObject;

  protected override OnLoad(EventArgs e)
  {
      someObject = new SomeObject();
      if(!IsPostback) someObject.DoSomething();
  }

  protected Button_Click(object sender, EventArgs e)
  {
     someObject.DoSomethingElse();
  }
}
with each request it's a new object. if you want to keep the current object between postbacks, then you have some options.
1. viewstate
2. session
3. application
4. cache
5. app domain

With some non-ms practices. you can use static objects and the application domain to keep an instance of objects between requests.

each option has it's own pros/cons. research the page lifecycle for more information

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
That's just how I was doing it but I wanted to keep the data in tact between postbacks.

in the OnLoad event I did someObject = Session("theClass") after someObject = new SomeObject();

Then in the Button_Click, I did Session("theClass") = someObject.

This is right?

 
is there a reason you need to persist the object between postbacks?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Yes, I am storing data in the classes variables.

I was also using the class to read and write to the database.
 
I also thought it would be a clean way of saving my application settings.
Not the application itself but the user's settings for the site.

Also the search boxes between screens.
I have 5 screens that are really the same screen but have different grids on them.
It would be nice for the user to be able to switch screens and still keep their search text.
 
I have 5 screens that are really the same screen but have different grids on them.
Why not make one page, with a multiview control that has a top portion for search criteria, and a bottom(results) area that shows whichever grid you need to display?
 
you could create a function to load the values into the new object. with a set of default values when the page is not posting back.

you don't need the same object to query the database. for example, get the id, fetch from database, load to screen.
on postback get the id, fetch from database, update values, save to database.

if you look at frameworks like structure.map, log4net, or nhibernate they all require configuration before using them. you can take this "hit" once by loading the configs on application startup and then referencing the object through the application.

example
Code:
public void Application_Start(object sender, EventArgs e)
{
   NHibernateConfiguration.LoadConfiguration();
}
Code:
public static NHibernateConfiguration
{
  private static ISessionFactory factory;

  public static void LoadConfiguration()
  {
     factory = new Configuration.Configure().BuildSessionFactory();
  }

  public static ISessionFactory InstanceOfSessionFactory
  {
     get { return factory; }
  }
}
Code:
protected void OnLoad(EventArgs e)
{
   if(!IsPostback)
   using(ISession session = NHibernateConfiguration.InstanceOfSessionFactory.CreateSession())
   {
      //get data from database
   }
}
all this to say that if you need objects to persist across multiple requests there are more OOP friendly ways than managing the objects on each page.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I'm sorry. They are all the same aspx file.

On my menu, it has 5 options for what the user wants to searc on.
marks and Numbers
Weights and measures
marketing
Nutritionals
etc

They are all the same file but the menu passes a variable to the aspx file to tell it what screen it's supposed to be.
based on this variable, it displays different headings and a slightly different grid at the bottom but the top portion always looks the same.

Problem is that when the user clicks a different menu option, it calls the page the ?fnc=M and the page thinks it's being reloaded so the textboxes clear.

 
There is no need for a menu. Just create one page, use a radiobutton list for the "report" they want. Based on that selection, load your headings and grid.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top