I've tried using Sleep but that just froze the program. Is it messing up because it's in the timer? I can get it to go to the site and not do code, but once I put other code it doesn't load properly.
I also tried putting code in:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) but then I had even more problems.
private void timer1_Tick(object sender, EventArgs e)
{
//code here
if (click == null)
{
webBrowser1.Url = new System.Uri("http://url.com", System.UriKind.Absolute);
timer1.Stop();
//load or wait x seconds
timer1.Start();
}
EDIT:
I'm trying to go from one page to the next and doing code once the page is loaded. If I put code in webBrowser1_DocumentCompleted it messes up the code before it. How do I add DocumentCompleted to only this one instance?
First, it seems like ur approach is wrong,
Open the url in the web browser control.
Wait for DocumentCompletedEvent. It will come once page is loaded fully.
Write ur code in document completed event based on your criteria/condition.
Related
I have a problem with my little c# project.
I need to somehow navigate through a site, performing a few simple actions on each page. My solution to it was along the lines of this:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var button = webBrowser1.Document.GetItemById("next_page_button");
button.InvokeMember("click");
webBrowser1.Refresh();
//here's my ugly solution which works
do {} while (webBrowser1.ReadyState!=WebBrowserReadyState.Complete);
webBrowser1.Navigate("http://www.webtest.com/page3");
webBrowser1.Refresh();
//same method of waiting for loading, causes endless loop this time
do {} while (webBrowser1.ReadyState!=WebBrowserReadyState.Complete);
var images = webBrowser1.Document.GetElementsByTagName("img");
//and then I do stuff with all them images..
So basically my program detects that the webBrowser loaded a page just fine the first time with that ugly while loop, but then, after the navigate() command it enters the second loop and never comes out of it. How come?
I've checked and double checked everything in debug mode, going through every step.
I could use your advice on organizing this program better for sure. xD
After two years i don't know it would help!!
but for others, the main thread is getting busy with your while loop so webbrowser object can not do anything, you need to implement this
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler((object sender, WebBrowserDocumentCompletedEventArgs arg) =>
{
/// do anything you want with webbrowser document.
})
or using Application.DoEvents(), this will make your main thread loop once and the webbrowser object load it's resources like javascripts files.
Ok i am trying to make my webbrowser(webbrowser1) get a url that is read from a textbox (textbox1 ) so the user can pick where the web browser connects from
i have tried:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Url = textBox1.Text();
}
And:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Url = textBox1.Text;
}
and some outer methods but i can not seem to get it to do it is it possible to do this for a web browser ?
(i mean a form web browser in the project not Firefox, chrome and so on ... )
You're handling the DocumentCompleted, which fires when the document has completed loading.
That's probably not what you want; you probably want that code to run when the user wants it.
You shouldn't override DocumentCompleted method. What you want to do is have a button, or in your custom textBox1 override lost focus or one of the action methods. With button override the click.
I guess you could override text changed in textBox1 and detect enter key pressed or some other trigger.
But regardless, you don't want to override DocumentCompleted
webBrowser1.Navigate(new Uri(textBox1.text));
would help you navigate to the URL string.
Here are some real simple examples:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.url.aspx
I'm using WebBrowser and when I'm trying to call .Navigate(some_local_html) then nothing is displayed on my browser. If I then use MessageBox.Show(), then while message is shown I can see my html in browser. But when I close MessageBox, html is missing again.
I've tried Try-catch, but there was no errors.
I was trying to set default url on webBrowser control, and there is no result also. I can see nothing.
RESOLVED:
That wasn't a thread itself, but some kind of thread. I added next code:
Stream stream = null;
webBrowser1.DocumentStream = stream;
and forgot to remove it... That's a reason.
Thanks everyone!
Not sure if this will help at all, but it sounds like something is redrawing in the background, as when you put a messagebox up I am sure it sleeps the thread so nothing else can happen until it has been actioned, so whatever is overwriting it would be stopped temporarily.
If you do have something on that thread refreshing or redrawing frequently that could be causing your problems, try adding a button to your form which does a thread.sleep(1000) to see if that correctly displays your browser for a second.
It'd be helpful to know where you are calling your navigate and MessageBox functions. I quickly created a test to see if I could produce a similar result but the code below worked exactly as expected.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate("http://www.google.com");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Some Text");
}
}
What I have going on is a Invokemember("Click"), the problem is I want to be able to grab the resulting innerhtml. The problem is i'm unsure of how/if it's possible to wait until the resulting action of the invokemember("click") is resolved. Meaning, in a javascript when you perform this click it will take you ot the next 20 items listed. However, i'm unsure of how to tell when that javascript will be fully loaded. Below is what I'm using.
private void button1_Click(object sender, EventArgs e)
{
HtmlElement button = webBrowser1.Document.GetElementById("ctl08_ctl00_InventoryListDisplayFieldRepeater2_ctl00_BlockViewPaging_Next");
button.InvokeMember("click");
HtmlElement document = webBrowser1.Document.GetElementsByTagName("html")[0];
}
One possible solution is to modify your "click" event handler in javascript so it changes the value of some hidden input field right before exiting the method (after all work is done). You can attach to the event of changing field from C# code and act when it's fired.
// Your init method - you can call it after `InitializeComponent`
// in the constructor of your form
Init() {
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
webBrowser1.Document.GetElementsByTagName("statusField")[0].AttachEventHandler("onchange", WorkDone);
}
void WorkDone(object sender, EventArgs e) {
HtmlElement document = webBrowser1.Document.GetElementsByTagName("html")[0];
}
That's the raw solution, I haven't yet checked whether "onchange" is a correct DOM event.
Also, you can't attach to DOM events before the document is completely loaded, that's why I put the attaching logic in the handler of DocumentCompleted event.
I'm experimenting with some AJAX now. I have a custom control which appears on my masterpage in which there is an update panel and a timer. The timer fires and the panel updates and everything is dandy. Except that there are some operations that I don't want it to perform on every refresh. It seems like the entire page lifecycle happens with each refresh. There are variables I want to set, and keep their value on the refresh. Is there a way to make it perform ONLY what's in the timer_tick call?
You could take a look at Request["__EVENTTARGET"] in the page load event to see what control caused the postback. If it's the timer control, jump out of the function.
Assuming your timer is called "refreshtimer":
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTTARGET"] == "refreshtimer")
{
return;
}
// etc
Not sure what what an AJAX.Net post back looks like to, But I usually protect my other controls and content by checking for post back;
protected void Page_Load(object sender, EventArgs e)
{
// if its a post back then my controls should already be setup...
if (!Page.IsPostBack)
{
InitControlData();
}
}
and then it should fall thru to your event handling?
protected void timer_tick(object sender, EventArgs e)
{
// Do my Ajaxy work~
}
UpdatePanels always force the entire page to refresh. If you want only a certain portion of the page to be processed (and it's a fixed size) then you could try using an iframe.
Alternatively, if you want to save the variables you can either put them in ViewState or SessionState so that they are persisted between postbacks
This is not possible with UpdatePanels... the entire page will go through the entire lifecycle. As others mentioned, you can limit the processing that happens by using IsPostBack or ScriptManager's IsInAsyncPostBack, but ultimately this is not going to be a great solution for complex pages.
However, you can use Page Methods to execute just one static method in your page, but you'll have to make the Javascript call yourself and update the UI. Here are some examples:
http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx
http://encosia.com/2009/07/21/simplify-calling-asp-net-ajax-services-from-jquery/
http://weblogs.asp.net/craigshoemaker/archive/2008/09/29/using-jquery-to-call-asp-net-ajax-page-methods.aspx