blah blah blah is here! blah blah » Close

up0down
link

If i click on a link from a webControl, i am redirected to a new page. How do i read the content from that new page in c#? with the help of javascript(is my guess)...but how exactly?
The idea is to click on that link like i am clicking on a normal button from c# with code generating in c#. It's even possible?

q12
349

no ideas yet?

last answered one year ago

2 answers

up0down
link

Judging by your previous posts, I assume you must be talking here about the Windows Forms WebBrowser control rather than an ASP.NET WebControl.

If you known the url that you will be directed to on clicking the link, then an easy way to do this would be to handle the WebBrowser's Navigated event and simply check that the new url is the same as the one you're interested in.

Suppose for example that the WebBrowser control is set to initially load the debugging.com home page and you want to know when the Users button is pressed. Here's how you could do it:

private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri("http://www.debugging.com");
}

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
if (e.Url.ToString() == "http://www.debugging.com/user/")
{
MessageBox.Show("Users button pressed");
}
}

You can obtain the content (i.e. the html) of the current page using the webBrowser1.DocumentText property.

q12
349

yes, you are right, im not into ASP.Net. Your little test worked like a dream. Thank you so much.

up0down
link

a little bug i encounter:

private void Form1_Load(object sender, EventArgs e)
{
StreamReader sr = new StreamReader("testMatritza2-html.html");
webBrowser1.DocumentText = sr.ReadToEnd();
sr.Close();
}

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
if (e.Url.ToString() == "http://www.w3schools.com/tags/ref_ascii.asp")
{
MessageBox.Show("Users button pressed");//this appear 2 times
//( 1 time before loading new page, and 1 time after loading new page)
}

//or if i use this:
if (e.Url.ToString().Contains("www.w3schools.com"))
{
MessageBox.Show("Users button pressed"); //this appear about 6 times
}
}

vulpes
17279

I think the Navigated Event is firing multiple times because those particular pages have multiple frames. Try handling the Navigating event instead which doesn't appear to suffer from this problem.

Feedback