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.
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 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.
I just want to control which button user clicked, so I use Session Variable to store that data because I have to create all dynamic control in the Page_Load (to allow event handler work properly). The problem is this Session Variable is not work at the first time I clicked but only the second time.
Here is my code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
'For the first time I want this session variable to be false because Image Button is not yet click
Me.Session("ImageClick") = False
End If
If Me.Session("ImageClick") Then
'Then after Button Image is clicked I try to add Dynamic control to a div
AddPreviewTable(0)
CreateButtomButton(Me.Session("totalPage"))
Debug.WriteLine(Me.Session("totalPage"))
Me.Session("ImageClick") = False
End If
End sub
The problem is I have to click on ButtonImage two times to turn Me.Session("ImageClick") to True.
Your if block is incorrect. You need to cast your session object into a bool:
If (Session("ImageClick") Is Not Nothing And CBool(Me.Session("ImageClick"))) Then
'Do your stuff.
End If
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.
Hey all, i am new at everything VB.net/ASP.net so i need some help with a problem i am currently having.
I have an ASCX.vb page that lets the user choose someone from a table. Once they do select someone, the page reloads with that persons information into another table below that one where they can submit an order for that person or remove that person.
Problem being is that i wanted to store the users ID that are selected but it seems that everytime the page reloads to show an update, it dim's my array again back to default.
This is my code once they choose a user:
Namespace prog.Controls
Partial Public Class progReportGrid
etc....
Public strIDArray() As String = {"0000770000"} 'Just a dummy place holder
Private Sub gvData_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvData.RowCommand
Dim idIndexNumber As Integer = Array.IndexOf(strIDArray, strID)
For i As Integer = 0 To strIDArray.Length - 1
System.Diagnostics.Debug.WriteLine(strIDArray(i))
Next
If idIndexNumber = -1 Then
ReDim Preserve strIDArray(strIDArray.Length)
strIDArray(strIDArray.Length) = strID
RaiseEvent EmployeeSelected(Me, New ESEventArgs(strID))
End If
End Sub
So everytime to page reloads the Public strIDArray() As String = {"0000770000"} gets called again and, of course, clears anything that was saved to it other than 0000770000.
How can i keep it from doing this?
UPDATE
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
'do what?
End If
End Sub
David
Perhaps you have a misunderstanding with the stateless model of a web application. Anything that is not stored in the ViewState or Session or a shared application variable will NOT persist through postbacks.
This means that when you declare your
Public strIDArray() As String
as a standard variable, it will be re initialized every time the page posts back.
For example, here's a simple flow of how this works:
The user opens their browser and opens up your aspx web page.
The request goes to your server, and all the controls including progReportGrid are instantiated as new instances and added to the page. (this is where your variable gets set to its original value every time)
A bunch of events are fired, including the Me.Load event
Controls that were added to the page are asked to generate their HTML
The HTML gathered from all the controls on the page is sent back to the user
So because your controls are all re-instantiated every post back, class variables like strIDArray are pretty much thrown out after the page is sent to the user.
Instead, if you want the page to remember what value the array had last postback and add more to it next postback, you have to use either ViewState, or Session.
For example:
Private Property StrIDArray() As String()
Get
If ViewState("StrIDArray") Is Nothing
ViewState("StrIDArray") = New String() {"0000770000"}
Return ViewState("StrIDArray")
End Get
Set(ByVal value As String())
ViewState("StrIDArray") = value
End Set
End Property
Now if you used this property instead of your variable, you could manipulate that array and as long as the user is on that page, the value of that array will persist across postbacks.
Use the Page.IsPostBack property in your Page_Load method.
This property is false for the first time the page loads and false for every postback afterwards.
If you set your default value strIDArray() = {"0000770000"} within If (Page.IsPostBack <> true) then it will not get reset on the postback.
EDIT : My VB syntax is VERY shaky but let me try it out
Partial Public Class EARNReportGrid
Begin
Public strIDArray() As String // DO NOT INITIALIZE it here. ONLY DECLARE IT
.....
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
//INITIALIZE the ARRAY
strIDArray = New String(){"00007700000"}
End If
End Sub
......
End