Will WebBrowser generate an error if this happens - c#

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.

Related

WP 8.1 Scroll to the top of the ListView

I would like to automatically scroll to the top of the ListView when the user navigates to this with the back button. I tried to use this:
scrollViewer.ScrollToVerticalOffset(0.0);
It works, but the VS says:
ScrollToVerticalOffset may be altered or unavailable for releases
after Windows 8.1. Instead, use ChangeView.
Okay, I thought it's no problem, I will use ChangeView instead of this. But the ChangeView doesn't work properly. At least in my case. I tried this:
scrollViewer.ChangeView(null, 0d, null);
It works when I call it for example from a button, but it doesn't do anything when I put it to the OnNavigatedTo method.
So why does it work?
If you take a look at MSDN description od OnNavigatedTo, you will see that it's called before the visual tree is loaded. Therefore if you want to manipulate your UI elements - do it in Loaded event:
You cannot use OnNavigatedTo for element manipulation or state change of controls on the destination page. Instead, attach a Loaded event handler at the root of the newly loaded page's content, and perform any element manipulations, state changes, event wiring and so on in the Loaded event handler.

Stop button from opening new tab/window upon exception

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

WebBrowser Control not added to Form

I'm using WBC in my project but I don't need it to be added to the form, I tried to call Navigate method but still get empty string when retrieving
the WBC .DocumentTitle!
Navigate is asynchronous, that means it goes to that web page on another thread and does not wait until it finishes. You are trying to get the Title immediatelly, but it is not set yet.
You should attach to DocumentCompleted event on the WBC and check for title there.
As noted the title can only be retrieved after it has been set. To know when that happens, in addition to DocumentCompleted event there is also a WebBrowser.DocumentTitleChanged event that is convenient and could simplify your logic.
Also, presumably, the latter event would also fire when title changes after the document is loaded via Javascript.

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));
}

How do I force and event to resolve from within another event

I have a Winforms App (.NET 3.X) That runs a method in a class to process some data. The method periodically raises a StatusUpdate event with a count of the number of items processed. I have a ToolStripStatuslabel on the Form that i would like to update with the count. The problem is that status label never updates with this count until the process is complete. Below is the code from the status update event handler
toolStripStatusLabel.Text = e.Count.ToString();
statusStrip.Refresh();
I think the problem is that the Refresh event is not firing because the processing method is being called from within a Button press event. I think there is a way to force the Refresh to process but I do not remember what it is.
My only other solution is to execute the processing in it's own thread.
Found the answer in another thread:
Call Application.DoEvents() after setting the label, but you should do all the work in a separate thread instead, so the user may close the window.
This is the command that I was thinking of...
Have you tried calling refresh on the label itself ?
toolStripStatusLabel.Refresh();

Categories