Help, anybody? I have a real problem here!

notlamc

Newcomer
Joined
May 2, 2004
Messages
7
Hi!

My VB.NET app hosts a Microsoft Web Browser control, which in turn
points to an ASP.NET site that I have written.

Everything works fine but I need to accomplish something no one seems to be
able to answer, at least not in a straight forward fashion!

Basically, users of my site download, install and run my VB.NET app.

They login to the ASP.NET site (via the Microsoft Web Browser control embedded in my VB.NET app).

They are then given an ASP.NET linkbutton to click.

What I need to do is this: When the user clicks that ASP.NET linkbutton, I need my VB.NET app to detect this and then invoke a function whilst passing the value of a hidden form field also on that page.

Does anyone have any ideas on this as I am really stuck.
 
maybe i'm missing something. You have an event for the linkbutton, right? if that's the case, run the hidden field as a server control and you can access the value from the code behind page.
if i'm off base, sorry. maybe some more detail would help out.

-lp
 
notlamc said:
What I need to do is this: When the user clicks that ASP.NET linkbutton, I need my VB.NET app to detect this and then invoke a function whilst passing the value of a hidden form field also on that page.

Does anyone have any ideas on this as I am really stuck.
in your form declare a method similar to this:

PHP:
public bool SomeMethod(mshtml.IHTMLEventObj ev)
{
  this.text = "Clicked!!!";
}

At some point, after the page is loaded, given the link is defined similar to <A ID = "managedLink" HREF=[some url]>, do something like this

PHP:
// grab the browser
mshtml.IHTMLDocument3 doc = (mshtml.IHTMLDocument3)axWebBrowser1.Document; 
 
// grab the anchor
mshtml.HTMLAnchorElementClass anc = (mshtml.HTMLAnchorElementClass) doc.getElementById("managedLink");
 
//create the delegate
mshtml.HTMLAnchorEvents2_onclickEventHandler ev = new mshtml.HTMLAnchorEvents2_onclickEventHandler(myMethod);
 
//assign the handler
anc.HTMLAnchorEvents2_Event_onclick += ev;
 
Last edited:
By the way. . . this topic belongs in interop not asp.net
look in help for handling events raised by COM source

PPS go c# its much easier. vb is a dog
 
Actually in SomeMethod, given the hidden Input is ID'd "TheHiddenElement" you do something like this. . .

PHP:
public bool SomeMethod(mshtml.IHTMLEventObj ev)
{
// get the doc. . . 
mshtml.IHTMLDocument3 doc = (mshtml.IHTMLDocument3)axWebBrowser1.Document; 
 
// get the hidden element
mshtml.HTMLInputElement inp = (mshtml.HTMLInputElement) doc.getElementById("TheHiddenElement");
 
// you now have inp.value to work with
}
 
Thanks for your help guys, but the biggest problem I have is getting the host VB.NET app to detect the click on the button itself.

I could probably manage without passing a value. I just need to detect a click on an ASP LinkButton.

Any ideas.
 
notlamc said:
Thanks for your help guys, but the biggest problem I have is getting the host VB.NET app to detect the click on the button itself.

I could probably manage without passing a value. I just need to detect a click on an ASP LinkButton.

Any ideas.
UGH!!!

That is the code I gave you. . .
Actually it is a C# app doing it. . .

Don't make me write a vb version of the same!!!
VB just really bites for this stuff. Everytime I program in it I am struck at how inferior it is.

but if I must. . . give me a minute.
 
Thank you so much for doing this.

If there was a rating system (which I think there should be), I'd give you 10 out of 10.

But it's given me a new problem to overcome. My website uses an iframe which your code didn't pickup.

I'm assuming I have to somehow 'point' the handler at that page.

If you coud spare a few minutes helping me resolve that one, I'd appreciate it.

If not, then thanks again.
 
Last edited:
PHP:
	Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
		Dim doc As mshtml.IHTMLDocument3
		Dim frm As mshtml.HTMLIFrame
		Dim anc As mshtml.HTMLAnchorElementClass
		doc = DirectCast(AxWebBrowser1.Document, mshtml.IHTMLDocument3)
		frm = DirectCast(doc.getElementById("myFrame"), mshtml.HTMLIFrame)
		doc = DirectCast(frm.document, mshtml.IHTMLDocument3)
		anc = DirectCast(doc.getElementById("managedLink"), mshtml.HTMLAnchorElementClass)
		AddHandler anc.HTMLAnchorEvents2_Event_onclick, New mshtml.HTMLAnchorEvents2_onclickEventHandler(AddressOf MyMethod)
	End Sub

If I have to write in it, I get the right to complain about it. . .
don't I?

Man I Hate VB!!! It Truly is an awful language!!!!
 
Last edited:
:0

Hi!

Thanks for that Joe Mamma, although it doesn't seem to work.

I replaced the old AxWebBrowser1_DocumentComplete sub with the new code (obviously changing the ManagedLink and Myframe to my names) but still no luck.

If you've managed to get this working in the form of a project, would you mind attaching what you've come up with please.

Many thanks,
Craig.

P.S Why do you hate vb so much lol

Joe Mamma said:
PHP:
	Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
		Dim doc As mshtml.IHTMLDocument3
		Dim frm As mshtml.HTMLIFrame
		Dim anc As mshtml.HTMLAnchorElementClass
		doc = DirectCast(AxWebBrowser1.Document, mshtml.IHTMLDocument3)
		frm = DirectCast(doc.getElementById("myFrame"), mshtml.HTMLIFrame)
		doc = DirectCast(frm.document, mshtml.IHTMLDocument3)
		anc = DirectCast(doc.getElementById("managedLink"), mshtml.HTMLAnchorElementClass)
		AddHandler anc.HTMLAnchorEvents2_Event_onclick, New mshtml.HTMLAnchorEvents2_onclickEventHandler(AddressOf MyMethod)
	End Sub

If I have to write in it, I get the right to complain about it. . .
don't I?

Man I Hate VB!!! It Truly is an awful language!!!!
 
notlamc said:
Hi!

Thanks for that Joe Mamma, although it doesn't seem to work.
hmmm. . . try this (note the frm.contentWindow.document)
it is something along these lines I dont have the time to code and test this right now. Do some investigating of the mshtml interop in the project I sent you


PHP:
Dim doc As mshtml.HTMLDocumentClass
Dim frm As mshtml.HTMLIFrameClass
Dim anc As mshtml.HTMLAnchorElementClass
doc = DirectCast(AxWebBrowser1.Document, mshtml.HTMLDocumentClass)
frm = DirectCast(doc.getElementById("myFrame"), mshtml.HTMLIFrameClass)
doc = DirectCast(frm.contentWindow.document, mshtml.HTMLDocumentClass)
anc = DirectCast(doc.getElementById("managedLink"), mshtml.HTMLAnchorElementClass)
AddHandler anc.HTMLAnchorEvents_Event_onclick, AddressOf MyMethod

notlamc said:
Hi!
P.S Why do you hate vb so much lol
VB is just so Bass-Ackwards as far as writing OOP code.
You have to type to much stuff to do simple things.
its clunky

the class files aren't structured in a concise manner.
Yes things have gotten better in VB.Net, but it is still clunky
For example having to type in a keyword DirectCast to cast variables is a pain in the rear. yes its just 9 characters but it is indicative of the type of crap you have to do in vb to do typical OOP tasks.

Just my opinion. But force yourself to learn and use C#, it wont take long to see the plusses.
 
Last edited:
Thanks!

Hi Joe Mamma.

Thank you for your reply, that is grand. Worked straight away but an unusual
problem.

It seems to generate two events for one click....

I know you said you don't have time to look at it, but if you do, and can think of the reason, I'd appreciate it if you could just slap it on here. If not, then thanks very much for all your help.

Craig.

Joe Mamma said:
hmmm. . . try this (note the frm.contentWindow.document)
it is something along these lines I dont have the time to code and test this right now. Do some investigating of the mshtml interop in the project I sent you


PHP:
Dim doc As mshtml.HTMLDocumentClass
Dim frm As mshtml.HTMLIFrameClass
Dim anc As mshtml.HTMLAnchorElementClass
doc = DirectCast(AxWebBrowser1.Document, mshtml.HTMLDocumentClass)
frm = DirectCast(doc.getElementById("myFrame"), mshtml.HTMLIFrameClass)
doc = DirectCast(frm.contentWindow.document, mshtml.HTMLDocumentClass)
anc = DirectCast(doc.getElementById("managedLink"), mshtml.HTMLAnchorElementClass)
AddHandler anc.HTMLAnchorEvents_Event_onclick, AddressOf MyMethod

VB is just so Bass-Ackwards as far as writing OOP code.
You have to type to much stuff to do simple things.
its clunky

the class files aren't structured in a concise manner.
Yes things have gotten better in VB.Net, but it is still clunky
For example having to type in a keyword DirectCast to cast variables is a pain in the rear. yes its just 9 characters but it is indicative of the type of crap you have to do in vb to do typical OOP tasks.

Just my opinion. But force yourself to learn and use C#, it wont take long to see the plusses.
 
I bet it's DocumentComplete event is being executed for both the Main form document being loaded and the iframe doc being loaded, hence the handler is being added twice.

maybe a private event handler variable can be declared

PHP:
private LinkClickHandler as mshtml.HTMLAnchorEvents2_onclickEventHandler

in the form load event initialize it:
PHP:
 LinkClickHandler = New mshtml.HTMLAnchorEvents2_onclickEventHandler(AddressOf MyMethod)

in the webbrowser's DocumentComplete event remove it then add it:

PHP:
Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete 
		Dim doc As mshtml.IHTMLDocument3 
		Dim frm As mshtml.HTMLIFrame 
		Dim anc As mshtml.HTMLAnchorElementClass 
		doc = DirectCast(AxWebBrowser1.Document, mshtml.IHTMLDocument3) 
		frm = DirectCast(doc.getElementById("myFrame"), mshtml.HTMLIFrame) 
		doc = DirectCast(frm.document, mshtml.IHTMLDocument3) 
		anc = DirectCast(doc.getElementById("managedLink"), mshtml.HTMLAnchorElementClass) 
		RemoveHandler anc.HTMLAnchorEvents2_Event_onclick, LinkClickHandler 
		AddHandler anc.HTMLAnchorEvents2_Event_onclick, LinkClickHandler 
End Sub

You might want to put some code in there to make sure that the HTML objects are actually assigned. And perhaps some code to be sure that the handler is only assigned on specific pages.
 
Back
Top