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.
Related
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 .
I am having problem in triggering Msgbox. It is not working in other browser than that of the iis server.
This is my code
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If (Not Session("user") = 1) Then
MsgBox("You are not signed in!")
Exit Sub
End If
End Sub
How to resolve this problem.
Thanks
Since you say in the browser, I assume you're using WinForms or MVC, etc. MessageBox doesn't exist in that context. Look into using Javascript or some client-side code to send a message to the user. Something like this should be close:
String cstext = "You are not signed in!";
Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", cstext, true);
or:
ScriptManager.RegisterStartupScript
Good luck.
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.
I have a web application (.NET 3.5) that has this code in Global.asax:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
LinkLoader()
PathRewriter()
PathAppender()
End Sub
I want all those functions inside to get called except for when it's an AJAX call back. So, ideally I would have it changed to:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
If not Page.IsCallback then
LinkLoader()
PathRewriter()
PathAppender()
End If
End Sub
But there is not access to the page object here. So, basically my question is:
How do I check if the request is an AJAX call back inside Application_BeginRequest?
Thank you very much for any feedback.
John,
Thanks for pointing me in the right direction. The solution is actually to check for Request.Form("__ASYNCPOST"). It is set to "true" if it is a CallBack.
Thanks so much for the help!
You should have access to the HttpContext.Current.Handler object which you can cast to a Page object and get Page.IsPostBack or Page.IsCallBack. Although in order to do this safely you need to first test that it is a Page object and not null:
With HttpContext.Current
If TypeOf .Handler Is Page Then
Dim page As Page = CType(.Handler, Page)
If page IsNot Nothing AndAlso (page.IsCallBack OrElse page.IsPostBack) Then
'Do something
End If
End If
End With
From my understanding all IsCallback does is check if the form has a post variable named __CALLBACKARGUMENT. You could check the form yourself in Context.Request.Form and that should tell you the same thing as IsCallback.
I have a wizard control in my asp.net 2.0 project, and it contains a few steps. The second step has a textbox with a standard requiredfieldvalidator control attached to it. When the user clicks Next and the box is empty, the validator complains, all is normal.
However, when the user uses the sidebar steps to skip to the next-to-last step, and clicks Finish, the validator is not fired, and the textbox is empty. In my backend, I have this:
Protected Sub wizard_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles wizard.FinishButtonClick
If Page.IsValid Then
...
Else
lblError.Text = "You missed some fields, please return and enter them"
e.Cancel = True
End If
End Sub
(lblError is a label on the complete page, but that's not really the issue)
This code does not work...
What is a good solution to this problem? Remove the sidebar and just not use it? Hardly the nicest solution...
It's far from perfect, but I'm now using this as an answer:
Protected Sub wzrdAddEvent_SideBarButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles wzrdAddEvent.SideBarButtonClick
If e.NextStepIndex > (e.CurrentStepIndex + 1) Then
e.Cancel = True
End If
End Sub
"if using the sidebar steps, only allow one step forward at the most"