keeping a class object alive beyond the life of the page
-
Hello All, I've a quick question. I'm developing a Multi tier web application with the business logic class separate from the ASPX pages. Now, what I'm having a problem with is: when a declare an instance of a business class in the code behind, this object gets destroyed and re-initialized every time the page gets posted back, what I'm looking for is a way to keep the object as is after its initial initialization so I don't loose the information contained in it. Thank you all in advance PS. The reason behind this, I have one main aspx page from which I load and unload several user controls, and before unloading any user control I gather its information and put in the business class.
-
Hello All, I've a quick question. I'm developing a Multi tier web application with the business logic class separate from the ASPX pages. Now, what I'm having a problem with is: when a declare an instance of a business class in the code behind, this object gets destroyed and re-initialized every time the page gets posted back, what I'm looking for is a way to keep the object as is after its initial initialization so I don't loose the information contained in it. Thank you all in advance PS. The reason behind this, I have one main aspx page from which I load and unload several user controls, and before unloading any user control I gather its information and put in the business class.
Store the object in the session-Collection (you could do this in a function handling the PreRender-Event of the Page to ensure nothing is changed afterwards). During PageLoad get the object from session if form is posted back else initialize a new object.
-
Hello All, I've a quick question. I'm developing a Multi tier web application with the business logic class separate from the ASPX pages. Now, what I'm having a problem with is: when a declare an instance of a business class in the code behind, this object gets destroyed and re-initialized every time the page gets posted back, what I'm looking for is a way to keep the object as is after its initial initialization so I don't loose the information contained in it. Thank you all in advance PS. The reason behind this, I have one main aspx page from which I load and unload several user controls, and before unloading any user control I gather its information and put in the business class.
Hi DNWDN, Keep in mind that the web is stateless, so everything will have to be recreated on each HTTP request. What you need, rather than a way to persist your business class, is a way to persist the
state
of your data between requests. ASP.NET gives you several easy ways to manage state, the 2 simplest being:- ViewState: the data is encoded and stored at the client
- Session state: the state data is held in memory on the server
Now, if your business object is not large, you could actually store the object itself in the Session object for each user, but keep in mind that this approach does not scale well for a large number of users. Also, if your business object is serializable, you can store it into the ViewState. Hope that helps! Marcie http://www.codeproject.com
-
Hi DNWDN, Keep in mind that the web is stateless, so everything will have to be recreated on each HTTP request. What you need, rather than a way to persist your business class, is a way to persist the
state
of your data between requests. ASP.NET gives you several easy ways to manage state, the 2 simplest being:- ViewState: the data is encoded and stored at the client
- Session state: the state data is held in memory on the server
Now, if your business object is not large, you could actually store the object itself in the Session object for each user, but keep in mind that this approach does not scale well for a large number of users. Also, if your business object is serializable, you can store it into the ViewState. Hope that helps! Marcie http://www.codeproject.com
Hello, Thank you all very much for your fast response. - For keeping the object in the Session, this is out of the question because the business objects are large. (That is actually why I posted this question; I was looking for a way other than the Session) - For the ViewState: that is a really good suggestion, I know that for the web controls you can access and keep the state from the ViewState, but can you shed some light ( few hints or a quick example) on how to accomplish the same for my business objects, I would really appreciate it. Thanks again,
-
Hello, Thank you all very much for your fast response. - For keeping the object in the Session, this is out of the question because the business objects are large. (That is actually why I posted this question; I was looking for a way other than the Session) - For the ViewState: that is a really good suggestion, I know that for the web controls you can access and keep the state from the ViewState, but can you shed some light ( few hints or a quick example) on how to accomplish the same for my business objects, I would really appreciate it. Thanks again,
Paul Wilson has a great article all about ViewState: http://aspalliance.com/articleViewer.aspx?aId=135 Marcie http://www.codeproject.com