Crystal Report Object not refreshing after IsPostBack - asp.net

This is my .Net 2 ASP.Net code that used to work when hosted on Win 2000 and IIS3.
'In Page_Load, if it's NOT a PostBack then remove the cached report object so the that code later is forced to rebuild it.
Under Win2000 and IIS3 when I clicked a link to load the Page fresh, in Page_Load it would call Session.Remove("ReportObject"), then in FillRptParams realise the Session("ReportObject") is Nothing and reload it.
I initially put all the Session code in to make sure that between Crystal Report page requests it wouldn't keep going to the DB, it would just pull the ReportObject from the session variable display the next page.
Now I've switched to Win 2003 and IIS6 I ALWAYS get the SAME report served up, even when clicking on the link as I used to which essentially causes IsPostBack to be false and remove the Session object.
I'm hoping it might some settings under IIS6 that can make it behave as it used to.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Session.Remove("ReportObject")
End If
End Sub
Sub FillRptParams(ByVal snavcode As String, Optional ByVal CrystalOrPDForEXCEL As String = "")
If Not Session("ReportObject") Is Nothing Then
bReportCached = True
Else
bReportCached = False
End If
oSqlCmd = New SqlCommand
If bReportCached Then
orpt = Session("ReportObject")
Else
orpt = New rptUsageSummaryNew
oSqlCmd.CommandText = "HOSP_RPT_UsageAllSummary"
oDS = oDataAccess.GetReportDataSet(Session("Group"), oSqlCmd)
orpt.SetDataSource(oDS)
'Cache the report object so we don't have to load it again next time
Session.Remove("ReportObject")
Session.Add("ReportObject", orpt)
End If
End Sub

Move your code in the Page_init event, not in the page_load.
And suppress your "if postback code" when you have moved it .

Related

page refresh detection code not working

I found a nice article on how to detect page refresh in this article: Detecting browser 'Refresh' from Code behind in C#, I tried to follow the code and use it in a web form application written in vb.net.
My problem is that when I refresh the page using the browser refresh button, the boolean IsPageRefresh, always remains false. So the code that I am trying to prevent from executing on page refresh, keeps getting executed.
Here is an example if my vb.net code including the converted code from the article:
Dim IsPageRefresh As Boolean = False
Protected Sub Account_reset_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
ViewState("ViewStateId") = System.Guid.NewGuid().ToString()
Session("SessionId") = ViewState("ViewStateId").ToString()
Else
If ViewState("ViewStateId").ToString() <> Session("SessionId").ToString() Then
IsPageRefresh = True
End If
Session("SessionId") = System.Guid.NewGuid().ToString()
ViewState("ViewStateId") = Session("SessionId").ToString()
End If
End Sub
Protected Sub Account_reset_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
If (Not Page.IsPostBack) Then
If Not IsPageRefresh Then
If Not String.IsNullOrEmpty(Request("key")) Then
If Not (Customer.SignInByResetKey(Request("key"))) Then
Customer.Messages.Add("Invalid password reset key", MessageType.Error)
FormsAuthentication.SignOut()
Functions.Redirect(SiteLink.ResetPassword)
Else
' Do Nothing
End If
Else
Functions.Redirect(SiteLink.ResetPassword)
End If
Else
'Do Nothing
End If
End If
End Sub
I would like to ask for help to try to identify where I possibly am making the mistake, or if there is a better method.
I saw many articles that show how to catch page refresh in code behind, but they use the PreRender method, and I cannot implement that solution due to how to website was built.
I hope I can get some positive feedback.
Many thanks.

Why can't I access Session values from Page_Init that were stored during a previous postback?

I'm storing Session values inside of Page_Load during postback, but I'm unable to access them in Page_Init on the next round trip. The gist of my logic is that the end user should be able to log in on postback during Page_Load where values are stored in the Session object, then on the next round trip I can grab those values from the Session object and initialize objects of a custom type. Initially I attempted to initialize my objects based on the values stored in the Session, but when I found that wasn't working I added some code in Page_Init to debug and found that I wasn't able to access them at all. The catch is that I'm able to access the values fine if I completely reload the page - but if I try to refresh or try to fire the postback button again it's as though the code is ignored.
I'm able to access other values in the Session with this logic (I have a "StartTime" value I'm able to get that's created in BeginRequest) but I'm not able to access the ones created during postback in Page_Load.
This is independent of my custom type as I've tried with hardcoded values and arrived at the same result.
Where my postback is handled:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Select Case Me.form_action.Value.ToLower()
Case "save"
If Me.tab.ActiveTabIndex = 0 Then
cart.Login(Me.login.Value, Me.password.Value)
UpdateRecord()
End If
End Select
End Sub
These methods update the database and store the session value:
Public Sub UpdateRecord()
cart.UpdateDB()
SaveCart()
End Sub
Public Sub SaveCart()
If Not cart.ID > 0 Then Exit Sub
Me.cart_id.Value = cart.ID
Session("seminars_cart_id") = cart.ID
End Sub
This is where I attempt to access the data in Page_Init
Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
If Session.Keys.Count > 0 Then
phDbg.InnerHtml = ""
For Each k As Object In Session.Keys
Me.phDbg.InnerHtml += String.Format("Session({0}) - {1}<br />", k, Session(k))
Next
End If
End Sub

BackgroundWorker – Waiting for it to finish or a timeout elapses

I have a web page which is made up of a form for data entry and a panel for displaying the results. This page is written in VB.Net and the website hosting it is in ASP.Net.
Normal usage of the page is as follows:
The user inputs the form with some data/filters
The user presses the button "Search"
A BackgroundWorker starts finding the solutions
The BackgroundWorker instance is stored in a static variable, as I don't care about multi-user scenarios, but I'm not tied to this choice and I can change this. Also, the search process is asynchronous, but I really don't need to display anything while it's in progess.
The BackgroundWorker stores the result in a SolutionStorage object.
My goal is the following.
When the BackgroundWorker ends, the solutions found must be shown on the page. However, if after a fixed amount of time (currently, three minutes) it's still running, I want to terminate it and display the solutions present in the SolutionStorage in that moment.
The code goes as follows.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsNothing(worker) Then
worker.CancelAsync()
End If
worker = New BackgroundWorker()
worker.WorkerReportsProgress = False
worker.WorkerSupportsCancellation = True
AddHandler worker.DoWork, AddressOf workerDo
AddHandler worker.RunWorkerCompleted, AddressOf workerComplete
End Sub
Protected Sub search(ByVal sender As Object, ByVal e As EventArgs) Handles submitButton.Click
worker.RunWorkerAsync()
Thread.Sleep(180 * 1000)
If worker.IsBusy Then
worker.CancelAsync()
Dim solutions = repository.getSolutions()
'' Display solutions
If (solutions.Count > 0) Then
SolutionsRepeater.DataBind()
End If
End If
End Sub
Protected Sub workerDo()
' Collect data from the form
' Build the SolutionStorage
' Start the search
End Sub
Protected Sub workerComplete(sender As Object, e As RunWorkerCompletedEventArgs)
'' Display solutions
If (solutions.Count > 0) Then
SolutionsRepeater.DataBind()
End If
End sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsNothing(worker) Then
worker.CancelAsync()
End If
worker = New BackgroundWorker()
worker.WorkerReportsProgress = False
worker.WorkerSupportsCancellation = True
AddHandler worker.DoWork, AddressOf workerDo
AddHandler worker.RunWorkerCompleted, AddressOf workerComplete
End Sub
Is this a correct way of doing things? Is this the better way of doing things?
Locking down an asp .net thread with a sleep call for 3 minutes is not a very good solution.
I would recommend you to build a background system that could do search in the background so your asp .net pages returns immediately. Then you could refresh your page every 10 seconds to
see if the search is still running, finish or failed.
Here is a little trick you might be able to use, but it has some limitations and warnings:
https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
Read the comments to see the warnings and limitations.
But since you don't care about multi user support, and I assume the number of visits to your page is very limited, I can't see a direct problem with your current solution. But as soon as you expand it, it might cause you some problems.

session variable set by checkbox not being stored/shared from page to page

I have a listview that's showing a long list. Each item has the ability to be 'hidden' or not. I want a checkbox to either show all the list, or not to show the hidden ones. The idea is that users will hide the older items they don't want to see any more, but may want to see at some point. I want to store the value of this decision in a session variable so if the user navigates to another page, then comes back, the ShowAllCheckbox will pre-populate to what the user has previously decided. Everything is working good, except i can't get the session variable to keep. It keeps going back to False. This is what I have:
aspx page:
Show Hidden: <asp:Checkbox ID="ShowHiddenCheckbox" runat="server" AutoPostBack="True" OnCheckedChanged="ShowHiddenCheckboxChange" />
...
<asp:ListView ...>
<!-- this list works fine, and pulls the correct records -->
aspx.vb page:
Protected Sub ShowHiddenCheckBoxChange(ByVal sender As Object, ByVal e As EventArgs)
' toggle the values
Dim CheckBoxField As CheckBox = TryCast(sender, CheckBox)
If CheckBoxField.Checked Then
Session("ShowHiddenRotations") = False
Else
Session("ShowHiddenRotations") = True
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'if i navigate to another page, and come back to this one, this comes back as "False". I don't understand how it could be reset to False.
Response.Write( "Session: " & Session("ShowHiddenRotations") )
'when page loads check if session variable has been set to show/hide the hidden rotations
If Session("ShowHiddenRotations") Then
If Session("ShowHiddenRotations") = True Then
'update sql query on select statement to show the hidden rotations
'update checkbox to show it as checked
ShowHiddenCheckBox.Checked = True
Else
ShowHiddenCheckBox.Checked = False
End If
Else
'not checked by default (ie don't show hidden)
ShowHiddenCheckBox.Checked = False
End If
End Sub
The Session variable always reverts back to False when i navigate to another page and come back to this one. My understanding of session variables was that they would pass their values from one page to another until the user closes the browser. Maybe there's another way of doing this, or something simple I'm missing. Any help is much appreciated! Thanks!
Is session-state enabled on your site? It can be disabled in a couple of different way, on a page level or even in web.config.
You should also be aware the Page_Load event fires for every request, before the check-box auto-postback happens.
I'm also a little confused as to what you're trying to store: I assume every row has a check-box, but it seems you're trying to store the set/not-set value in a single session variable. How do you differentiate which have been selected, and which ones hasn't? :)
Update:
Okay, let's try a clean the code up a little bit. First create a property to access the session value:
Private Property ShowHiddenRotations As Boolean
Get
If Not Session("ShowHiddenRotations") Is Nothing Then
Return CType(Session("ShowHiddenRotations"), Boolean)
Else
Return False
End If
End Get
Set(value As Boolean)
Session("ShowHiddenRotations") = value
End Set
End Property
If you're using that value on other pages, I would recommend moving it to a seperate class.
Then we can reduce your other code to something closer to this:
Protected Sub ShowHiddenCheckBoxChange(ByVal sender As Object, ByVal e As EventArgs)
ShowHiddenRotations = ShowHiddenCheckbox.Checked
End Sub
And ...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If not Page.IsPostBack
' Load your data, better stick it in a seperate sub...
ShowHiddenCheckBox.Checked = ShowHiddenRotations
else
' This section is executed BEFORE any control methods are run, i.e. ShowHiddenCheckBoxChange
end if
End Sub
I'm guessing your problem is really just the order of how things are called in your page. What happens when you debug through it?

Possible - cancel user control rendering if not loaded in x seconds?

This is probably a lark, but for the recaptcha control as it sometimes takes a long time to render, is this possible?
If it takes more than say 5 seconds to render, I'd like to stop the rendering of the object and display my own captcha.
I'd start a timer on page load and if 5 seconds has elapsed, in some event in the recaptcha control (prerender?), I'd cancel the render or make it invisible or something to that effect. It is a 3rd party user control, so I don't have the source.
Update:
I tried the code below after I posted. It sort of works in that if the user control can't connect its server, ie - I turn disconnect my internet connection, but it doesn't sense when there is a really long pause when the control has waiting for the server to get back to it. Even if I change the millisecond interval to 1, the control renders.
<MTAThread()> _
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim ucChk As New UCExistenceChecker(recaptcha, Me)
Dim doFindUC As System.Threading.TimerCallback = AddressOf ucChk.FindUC
Dim stateTimer As System.Threading.Timer = New System.Threading.Timer(doFindUC, Nothing, 0, 5000)
End If
End Sub
Public Class UCExistenceChecker
Dim _r As Recaptcha.RecaptchaControl
Dim _pg As Page
Sub New(ByVal r As Recaptcha.RecaptchaControl, ByVal pg As Page)
_r = r
_pg = pg
End Sub
Sub FindUC(ByVal stateInfo As Object)
If _pg.FindControl("recaptcha") Is Nothing Then
_r.SkipRecaptcha = True 'This "unrenders" the control, sort of.
End If
End Sub
End Class
You may be able to use an iframe to contain the captcha block and subscribe to the either the readystatechanged or layoutcomplete events. You could then use setTimeout() to schedule some JavaScript to run after the maximum time you care to wait, and if neither of these events has fired, remove the iframe from the DOM and replace it with your own.

Resources