Crystal Reports Load Unload Dispose Events - asp.net

I have an ASP.NET app that uses Crystal Reports through the CR Viewer.
The records shown are based on a session variable.
This part works fine.
However, I want to clear the session variable when they are done looking at the report.
So I tried to clear it in the page_unload event. To my surprise, that event fired while the report was loading up. The first page of the report still displays OK, but additional pages don't as the records it was based on is now empty.
Further testing showed that the page_unload event doesn't even fire when leaving the CR
I don't know why the page_unload fires after the page_load, but I figured I would try clearing my session variable in the page_disposed event. But that didn't fire either when I went to another page in my app.
I also tried the CrystalReportViewer1_Unload event, but that also fires on every page when loading.
So I'm looking for where I can clear the session variable when I know they are done looking at the report. Thanks.

If anyone comes up with an answer, that would still be great. But here's the work around I found.
The reason I wanted to clear the session variable was that it was storing a shopping cart, and after printing, I wanted the shopping cart empty in case they went shopping some more.
What I ended up doing was copying the session variable to another. I clear the fist one before printing use the second one for printing the records. It doesn't matter if the second one is cleared, or not, as it's not the real shopping cart.

Related

Keep history of old page

I am running some stored procedure and on the basis of that i am doing some calculation and then display their counts and their images on the basis of the data.
then I have each button where I am moving forward with that Detailed data.
Now If I want to come back on this page with the old data then what should I do?
I have been already tried history.back(-1) or history.back() etc.. It will take me back but not with data and only with the control.
I need to come back with the data.
Please help me out.
Thanks.
save it to Session.
and in the PageLoad - Reload it from the Session.
history.back() just tells the browser to navigate back to a previous page... the exact page the browser originally saw (i.e. it will not fetch it from the server again with possibly changed data).
It's better to navigate forward to the page when you want to see it again with updated values. Redisplay the page when you revisit it, this time with the appropriate data. As Royi suggests, you can save that data in the Session.
You could always load the detailed data into the current page with an AJAX call when you click your details button. This way you preserve the state of the original page.
When navigating "back" just unhide the div that contains the initial view of the data.

Maintaining State Within Pagination

I am currently working on a site for an auto dealer and ran into a bit of a snag when I started trying to ad pagination to the inventory page.
The inventory page has 5 search options type, make, year, model and trim. They are all drop downs and they are all populated by a live feed, changing the options autopostback the page and the inventory/nav are updated accordingly.
Everything was working great until I went to add pagination to the page. The pagination works fine for what it is, if there are more then 15 results it adds a numbered nav to the page that allows the user to jump pages.
4
The problem is when they do jump pages all of the search options revert to there original settings, and when the page loads its pulling in every car again instead of what the user searched for. I can't really figure out what the issues is, I expect the state is getting lost when the user clicks the link even though it is to the same page. I tried using sessions but it seems to have muddied it up, does anyone have any suggestions for a fix?
If you require the code I can link it, I warn you though it is a pretty large amount of code and I don't think you could find much in it, but who knows.
Try using a LinkButton control with an OnClick event. Do whatever you are doing when you test for the "page=4" querystring parameter in the OnClick event. You are losing state because the a regular html anchor tag causes the page to load as if it were the first time you visited and ViewState is blown away - Page.IsPostback is false.
If you don't want to use a sever side control, you could always pass the search parameters in the query string.
4
If you do it this way, please be sure to sanitize your inputs before you pass them back to the database.

How to Persist a Control's Value/Text Property Between Page Refreshes?

I have two controls of interest on a page: a DropDownList and a Button. If the user presses the button, he gets a popup form which, when completed, causes the base page to completely reload, losing the value in the DropDownList. To clarify, it's a refresh, not a postback. I cannot change it to make it a postback. What is the most straightforward way of persisting the selected value in the DropDownList across this page refresh? I cannot avoid the page refresh, because the data entered in the popup is reflected elsewhere on the base page.
Update
I've cobbled together a tenative solution that I'm not terribly happy with: when the user clicks the button, I use javascript to get the current value of the dropdown and pass that with the querystring to the popup being loaded. When the user clicks "Save" on the popup, which causes the data to be saved then the base page to be reloaded, I first store the querystring value into a Session variable. The base page, on loading, looks at said Session variable. If a value is present, it sets the value of the dropdown accordingly and deletes the Session variable.
Although somewhat kludgy, it's the best I can come up with. I know that my Gracious Benefactors dislike using Session variables, but given the page reload, I cannot come up with an alternative. Also, since the Session variable is short-lived, being created shortly before a page is closed then being deleted shortly after the succeeding page is opened, I'm hoping this will be acceptable.
Contrasting opinions, refutating my solution or reasoning, are enthusiastically welcome.
Conclusion
Ultimately, after I described the solution to my Gracious Benefactors, we agreed upon an alternate approach: if a selection is made in the DropDownList, it must be saved before opening the popup. This avoids the whole ViewState problem altogether.
For updated question:
The solution you are trying sounds like it should work. It's not the sort of thing you want to use SessionState for but there are requirements here that make the situation out of the ordinary.
Depending on how you are getting the base page to reload you may be able to add a query string to that which would save you from putting hte value in the SessionState. But I imagine you have probably considered that.
But basically, as Postback values and viewstate are out of the question, SessionState and query strings are you only real option. Oh, unless you are allowed to use HTML5 local storage? (probably not)
From before question update:
If the aspnetForm is being posted back the the value should be persisted automatically by means of the postback values. So the first thing to know is whther your form is going through a postback or if you are refreshing the page in some other way.
If you can't postback the main form then as rockinthesixstring said an Ajax post might be what you need.
Also, if you are doing anything fancy with binding the datasource to the DropDownList or trying to persist the selectedValue yourself then check and re-think that.

Page_PreRender fires twice on first load in session

When I run the web application, I notice that Page_PreRender is fired twice. This only happens the first time in a new session. It does not happen if I refresh the page, or on postbacks.
I use .NET framework 3.5 and the built in ajax functionality.
The problem is not
related to img tag with empty src
attribute (which I have seen other posts with similar problem
has mentioned). I know this because I see this in
both FireFox and IE. The posts I saw
about this stated that this was not a
problem in IE. I have also searched
and found no img tags with empty src
in the generated page source, so it
should not be this.
I have also made a simple test
page where I have included some of
the functionality, and this does not
happen.
Have anyone any suggestions on what happens?
Note:
It is the entire page cycle that is firing twice, not just render.
I've experienced it; it's probably not what you are experiencing, but I'll enter it here anyway; I've noticed it when the application does a Response.Redirect at the PreRender level, which a redirect does not stop current execution, but makes it appear the event happens twice...
Again, probably not related, but including it just in case.
I copied some code that states:
Page.LoadComplete += new EventHandler(Page_PreRender)
I did not realize that this code fire for second time my Page_PreRender event.
When I comment it, never fire twice.

Why does my session variable appear to empty itself in ASP.NET?

I have user control on a ASP.NET web page, which contains a GridView and a radio button selector.
In the underlying middle tier I have a thread which goes to the database and then raises an event to say "I have some data" my User control handles this event and sets a Session Variable.
This works and I can see the event being handled and the Session variable gets the new data.
However when i go to use this session variable when the selected index of the Radio button selector changes the Session variable reports as "Nothing"
I have ensured that the obvious (i.e. spelling, Sessions switched on etc) are correct.
The GridView and radio button selector are encapsulated in the same Update panel.
Check that if your UpdatePanel - updatemode is set to 'Conditional'? also Child as triggers? I would first start by putting a stop in your page load, see whats happening from there. Do a search for all places where you populate that session variable and put a stop. You may be surprised, I have often found that page lifecycle gets confusing even though I thought I understood it. Alt - post some code and we can step through.
Yeah, sounds almost like a problem with order of operations or not checking for postback on a page load or something?
Like JamesM suggested, running your website in debug mode should really help identify the problem. You can bring up your watch window and set it for the Session variable you're looking for, then set breakpoints all over and check the value at each stop to divide and conquer the code.

Resources