Stop button from opening new tab/window upon exception - c#

I have a button that uses
OnClientClick="document.forms[0].target = '_blank'; window.setTimeout(fixform, 500);"
to allow the OnClick event to Response.Redirect to a new window. "fixform" sets the target back to normal. This works great when everything goes according to plan. How do I stop it from opening a new window when an exception is thrown on the OnClick event? I've tried Thread.Sleep for 500ms for the form to fix itself, but it still opens the current page in a new window, with the exception.

How do I stop it from opening a new window when an exception is thrown on the OnClick event?
You don't, because it doesn't execute that event until after the new window is opened. The client-side code and server-side code are executing at entirely different times on entirely different machines. The Thread.Sleep() for example happens on the server, after the request has already been made. What's happening here is:
Browser opens a new window
New window makes a request to the server
Server throws an error and responds to the request with the error
Without knowing more about the overall structure and user experience being achieved here, it's hard to advise. What should happen in the event of an error? Can the error be more meaningfully handled so that at least something useful is presented in the new window? (That is, the exception itself shouldn't be visible in the new window, and should never be visible to a user, but rather some page which handles it.)
If you want the new window to close in the event of an error, then one possibility could be to emit some JavaScript like this to the page in the event of an error:
window.close();
That way if there's no error, the window stays open. But if there is an error, the client-side code closes the window. (Since server-side code can't close the window, and indeed has no concept of a "window" in this case.)

Convert OnClick method to WebMethod, returning url instead of redirecting to it.
Call WebMethod from JavaScript via OnClientClick event, returning true/false to cause OnClick to fire only if the call succeeds, updating a hiddenfield value if it does
New OnClick event Response.Redirects to hiddenfields value

Related

Is there a way to let an asp.net page "catch up" to itself?

I'm working in C# and I need a button to become instantly disabled when a user clicks it. However, if I put as the very first line in the OnClick function
MyButton.Enabled = false;
it does nothing. The button remains enabled until it hits some sort of stop, whether it be the Catch block or the end of the function.
I was thinking that in VB/VBA you can use DoEvents() and that allows the code to catch up to itself. However, in C# there doesn't appear to be a DoEvents method.
This is a little different than the linked question, in that my OnClick code looks like:
OnClick="btnSubmit_Click"
and that user's OnClick code looks like:
onClick="this.disabled=true;
this.value='Sending…';
this.form.submit();"
When I tried to change my code to:
OnClick="this.disabled=true; btnSubmit_Click"
I got an error.
Compiler Error Message: CS1041: Identifier expected; 'this' is a
keyword
How can I do this in a C#/asp.net environment?
OnClick is a server-side event of the Button. So you cannot write:
OnClick="this.disabled=true; btnSubmit_Click"
OnClick accepts only the method-name of the server-side event handler.
If you want to handle the client-side button-click event to prevent that the user can click on it multiple times use OnCLientClick:
OnCLientClick = "this.disabled=true;"
You also have to set UseSubmitBehaviour to false.
Read: Disable a button control during postback.
You will need to utilize client-side JavaScript. One way is to utilize the OnClientClick property of your asp.net button control.
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick(v=vs.110).aspx
This is server-side and will be executed on PageLoad/Postback -
MyButton.Enabled = false;

Keep a form opened waiting for asynchronous method response

I have a question regarding a somewhat simple problem, but I think with many solutions and I don't know what would be the best.
I have the following scenario including:
ApplicationForm
Proxy (Client -> Server side)
Possible error message dialog.
The form sends a request to do something on the server side and the form closes (this is the current implementation). I changed this so the method that is being called on the proxy, server side returns a string value that contains an error message if something fails.
The problem is that I want to display a message box when the response comes back if the message is not String.Empty
I do the following:
Call method and assign value returned to a property field.
Create a timer that has an event that fires after 4 seconds.
... Meanwhile the form closes...
Timer event fires, Check the property for a possible error in the string and fire another event that calls a method on the form and displays a message box.
The thing is, I would like to keep the form opened and after the user clicks OK on the message dialog box the form should close.
Any idea is good.

C#.NET mnemonics unexpected behaviour

I have few buttons in my windows application which has mnemonics.Now if i press multiple keys the events of the button clicks gets fired as i have used mnemonics.
But the behaviour is not as expected,because second button event handler is getting executed before the first button event handler has finished its execution,and also i have written my code in such a way that in the event handler of first button i am disabling my second button,still the second button event handler is getting executed.The problem here is due to mnemonics.PLease suggest me a better way to handle mnemonics as soon as possible.
Thanks in advance.
If your logic is tied to your buttons, change that. Let your buttons merely manipulations an object that implements your logic. You can then check in this object whether the requested action is allowed (i.e. whether the previous operation has finished.
Alternatively you can just disable buttons when running and re-enable then when.finished.

Will WebBrowser generate an error if this happens

IF a WebBrowser control is doing a Navigate() command, and before it finished I call another navigate command, does this cause on error does it simply start a new navigation and dump the old one?
Thanks!
The first Navigate command will simply be dumped. Naturally, it may however cause problems if you have event handlers following the initial Navigate that are still executing, expecting the page from the first Navigate.
It simply start a new navigation and dump the old one.
I won't produce any exceptions as long as you are not depending on custom code executed on the Navigating or ProgressChanged event handlers. The WebBrowser just cancels the current navigation and starts a new one.

Webbrowser control in C# weird behavior

I'm setting a HTML string value to webbrowser.DocumentText. Problem is, sometimes it worked and sometimes it hangs up and not go through the documentcompleted event. what might be the problem?
Please reply, asap..
Thanks,
Jepe
Just a thought: With all the web-'dynam-ism' nowadays, a page is never really in a 'completely-loaded' state. After DocumentComplete, the OnLoad event of the page is fired, then any scripts placed in such an event are fired, and then javascript timers may be used to initiate download/upload of some resource, or a change in page using AJAX, etc.
I believe AJAX calls could cause all kinds of unseen problems with your WebBrowser control; because AJAX can cause multiple Navigating() events to fire during the loading of a page. And for that reason your app would experience multiple DocumentCompleted event fire on that page.
A possible solution could be to monitor the page changes and attempt to find out what possibly got changed. And to do this you may need to look into Notify method.
Called by MSHTML when a change occurs
in the contents of the markup
container with which it is
associated.
This and this discussion may help you coming up with something like following:
HtmlElement target = _webBrowser.Document.GetElementById("somedivthatwillbepopulatedbytheajaxrequest");
if (target != null)
{
target.AttachEventHandler("onpropertychange", new EventHandler(handler));
}

Categories