I have a login page and I want the login button to disable while the SQL query is checking whether it's a valid account.
This is so that you can't press the login button twice or more times while it's already trying to do it's thing. Just like any decent software.
When I call
signinButton.IsEnabled = false
in my signinButton_Click method it doesn't disable it until after my SQL has returned...
If I call MessageBox.Show("called") it will display it instantly, as expected, but for some reason that's not the same with IsEnabled.
Do I really need to use a seperate thread for this? It seems overly complicated, all I want to do is disable the button instantly, and not after the SQL has finished.
Much appreciated
Consider using async versions of your database calls with the await keyword. This allows your UI to be updated while query results are loaded.
Related
Actually i am getting a dataset from DB but it is consuming sometime (>1 minutes).
So i use ajax to run asynchronously to check whether the dataset has returned result and at the same time displaying a waiting page.
However , for times, if the user want to navigate to other page instead of waiting the returned result, is there anyone who can give
me a hint on doing that?
I would first try to optimize your query from the database. Any query that is running > 1 minute is is either ridiculously huge (which you could implement pagination to circumvent) ... or is in need of some optimizing perhaps even the indexing on the tables as well.
If that still doesn't yield fast enough response times then here are two approaches for your problem:
Whenever the user wants to hit this long running query, run it in a pop up window. This will allow the user to continue navigation in the main web app.
Take your pages and start showing them in an iframe. You can then make the request in the parent frame while the user is navigating other pages (which are being loaded in the child frame). Since the parent frame won't be reloaded in this scenario, the ajax request can complete uninterrupted in the parent frame.
Whatever the scripts and codes your are using in this scenario will not help you!
Better optimize your Database model.
create tables with clustered index.
use stored procedure,
if that too slow create your own Function and Views.
Users will not view more than 10,000 records on a single page. Depends on the process can restrict your data and its optional.... but not the right way!
Once you have sent any request you cannot stop it or navigate when loader is running up.
for Asynchronous data Processing Loader (waiting page) is not Required.
Try this hope It will Help....
In my C# code, I am using Watin to navigate the web, to log in to a page, I need to click the log in button, but right after I want to log out, so I have the click log out button right after, but the log out part doesn't work. I even tried closing the browser (using the close method) after logging in, but it didn't work. It feels like as soon as the page gets changed (i.e. after logging in) no more commands from the c# will work.
Does anyone know whats wrong?
As mentioned in another answer Thread.Sleep(milliseconds) is a way to wait for a time period for something to load. Very, very easy to implement, but it is far from optimal due to varying load times, and if you make it long enough so that it will always wait long enough you'll end up with a lot of wasted time. On one test this is not a big deal, but for instance if you have to wait 5 seconds and you have 1000 tests.... etc etc etc.
The route I've gone is:
Put in Thread.Sleep()s to determine if it is a "wait" issue.
If the the code with the Sleep() is going to be used more than once figure out what is causing the need for the sleep().
Refactor out the Sleep() using various Wait...() methods. WaitTilExists, WaitForAttributeEqualsWhatever, WaitForAsyncToFinish <- Not real methods, but WatiN has a bunch built in
The big cause of waits for me now is JQuery asynchronous calls in ASP.NET and I made a static helper class that works well for me to wait for async calls to finish. These tend to be very specific to what framework(s) the sites you're testing are written in.
The watin click command wait until the browser is loaded so practically it wait for the postback.
In case if you using ClickNoWait() command it will not wait.
So if your code looks like this it should work:
browser.GoTo("www.your-site.com");
// fill user/pass
browser.Button(Find.ByClass("login-class")).Click();
browser.Button(Find.ByClass("logout-class")).Click();
In case it's still not working you can add this after login click browser.WaitForComplete();
In Watin you will encounter many situations where the code is non blocking (you'll execute a line of code and will immediately keep going) so for those cases you'll need to find a different way to know that the next page (action, etc.) is already there. For example, on a login page you could check if that pages has a TextBox called UserName:
<code>
TextField uName = browser.TextField(Find.ByName("userName"));
if(uName.Exists)
{
// Then do the login code....
}
</code>
In the same way you should control that the page after the login is there before you keep going executing your code. So for example, if you are logging in into a page that you know that will contain the text: "Your Account Details" you might do something like this:
<code>
browser.GoTo("http://www.yourdomain.com/login.aspx");
//do your login code
browser.WaitUntilContainsText("Your Account Details", 240); // the second parameter indicates the seconds it will wait before it times out.
// your code to deal with the page after the login.
</code>
Using Thread.Sleep is a recipe for confusion and that's a problem for sure, you will NEVER get the timing right with a web page (even if you think it will take 10 seconds it might never come back and at that point the server will be terminating the opened connection).
Hope it helps.
Use Thread.sleep in your scripts to sync with logout and login...
or
instead of logout you directly close application and use ie instance to relogin to application
I am trying to code an app for work where our clients can edit certain fields. When they click edit, the code will lock the data to be editted. And when they click save the data will unlock. But I am having trouble deciding if I can unlock the data when they click elsewhere, go to another page or site, or even close the browser.
What's a good way to decide if the user has left the edit page?
(We are using C# .NET 3.5 and JQuery and a SQL Server 2005 DB).
If you really must use pessimistic locking you could add check in Session_End and unlock all locks that this user set in current session. Use onUserExit jQuery plugin to end session if user closes the browser window or goes on another site, here is the example :
https://stackoverflow.com/a/10481972/351383
You can make use of "onunload" event of html tag. This event is raised
- when Page is closed using X button
- when Page is redirected(In your case user clicks on edits and move on to different link without saving.)
Hope this helps!!
Your question is being understood as "what's a good way to decide if the user has abandoned the edit page without having clicked 'Save' to unlock the field?"
But I am having trouble deciding if I can unlock the data when they click elsewhere, go to another page or site, or even close the browser.
All the scenarios in which someone looks at another page from the same application might mean another window in the same session - perhaps to check something. You don't want that to trigger an unlock because your user could still have a window open in which they can submit the edit.
The other scenarios where the user leaves the page can use onUserExit.
However, you omit from this list the scenario "user goes to lunch leaving webpage open". This is why pessimistic locking is problematic.
Therefore, rephrase the problem as "the user has clicked Edit, but then failed to make the amendment and save within a reasonable time limit".
Basically, your lock should at some point expire.
Upon clicking 'Edit', the save lock kicks in and a timer (5 minutes?) starts, which is also visible on-screen. (If getting these in sync is an issue, make the client-side timer shorter than the server-side one). Your users must make the change and click Save within this time. You could perhaps add an Extend button to extend the time.
The principle is that you need to determine that someone has left it too long, not that someone has left it. You also show to users that having clicked Edit you are expecting them to make an edit some time soon.
You will need to code your system to unlock fields where the lock has expired.
This has the benefit that your countdown will look 'cool' to the sorts of people that request such edit locks.
I need to ask the user if he/she wants to continue an operation (say, save operation).
So, after the user clicks the Save button, some stuff is checked on the server side. If one condition is met, the user must be asked if he/she wants to proceed.
Based on user's answer, the postback should be automatically performed carrying the user's reponse back to the server, so the server will now ask again. Is it possible to do it?
Thanks.
EDIT:
To be more specific, I want this:
The user clicks Save button. The postback is performed
I need to make some validations/checks on SERVER SIDE (this is important!)
In the middle of the postback I want to stop if a certain condition is met and ask the user if he/she really wants to continue.
If the user clicks Yes, I need to re-post the request, but now I need to carry the user's response.
If that "certain condition" is met again, I will just ignore it because the user wanted that.
So the solution to add the confirmation dialog right when the button is pressed is not an option because the checks are not simple and require some complex stuff involved (impossible to do it on client side).
Ajax/JS/Telerik, all OK.
You can use
btnExample.Attributes.Add("onclick", "javascript:return confirm('continue?')";
just one of the options...
EDIT:
for your needs you will want to use AJAX, call a method on the server and upon callback open the confirm window.
2nd Edit:
if the server side work isn't long I would this using AJAX.
AJAX works asynchronously and you want a synchronous procedure, right?
instead of posting back, call an AJAX method from the javascript, which will look something like this:
Service.ProccessRequest(data, OnSucceedJSFunction, OnFailJSFunction);
this way when the server side method finishes the OnSucceedJSFunction on the JS will be called.
in this function you can do something like
if (confirm('are you sure?'))
{
call another server method...
}
if you need to resend the data to the same server side method or to another server side method you can do this again and call a different OnSucceed js function.
Im not sure though about what you want to happen at the end of all the procedure...
Quick and dirty is to use javascript confirm function, tons of example on how to avoid the postback when user clicks cancel or ok.
if you need better ui control surely you can use javascript / jquery or whatever to show a nice dialog box and prevent the postback to happen or invoke it.
If non-JS users do not bother you then something like jQuery or AjaxControlToolkit could provide a modal popup solution.
If you are conscious or perhaps work for a company that needs to provide non-JS solutions then you could consider sending the user to another page asking for confirmation, you could carry any relevant info in either the Session or the Query String.
I have an ASP.NET (C#) page that has a long load time (like 2 minutes). The user is presented with a little animation and a "please wait" message. If the user accidentally loads this page, they need to wait for it to load.
My question is: Is there a way to stop the page load?
Thank you
If you want to stop the server side processing then its a tricky operation. Generally once a request is made that page is rendering on its own independant of other thigns going on. What you would probably need to do is re-engineer that page to check at regular intervals whether a stop command has been issued and abort whatever it is doing at that point. The stop flag could be put in session and should be cleared out after the stoppage.
You may also need to consider how to properly identify the right one to stop (in case there is more than one running). This could be done by returning a unique ID that can be used in part of a call to the "abort" page.
My approach though rather than this complciated rigmarole is to make efforts to stop the user from making this accident. Possibly make whatever link they are clicking pop up an alert saying "the following page will take several minutes to render, do you wish to continue" and then hopefully you will effectively be aborting the page request before it is even made.
I should note that I've never tried to do this sort of thing before so there may be easier ways to do it but this is how I'd probably think abotu going about the problem.
Try window.stop() in JavaScript.