HTML Events in C#

Hello,

My application uses the Web Browser ActiveX control in C# 1.1 WinForms.

I use the following code to access the DOM:

mshtml.HTMLDocument            htmlDocument;
mshtml.IHTMLElement            htmlElement;
mshtml.IHTMLInputButtonElement      htmlInputButtonElement;

if (this.wbAdministrator.Document != null)
{
      htmlDocument = (mshtml.HTMLDocument) this.wbAdministrator.Document;
      htmlElement = htmlDocument.getElementById("btnQuoteSubmit");
      if (htmlElement != null)
      {
            if (htmlElement is mshtml.IHTMLInputButtonElement)
            {
                  htmlInputButtonElement = (mshtml.IHTMLInputButtonElement) htmlElement;

                  System.Windows.Forms.MessageBox.Show(htmlInputButtonElement.name);
            }
      }
}

I want to be able to trap the ONCLICK event of this button and various events of other elements. The htmlInputButtonElement object does not expose event delegates. How do I achieve that?

Thanks,

Khan
raheelasadkhanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
What does trapping the ONCLICK event do for you?

Bob
0
raheelasadkhanAuthor Commented:
I want my application to read the values for the form fields (text boxes) as soon as the user clicks the "Submit" button. This is not a dynamic page so the type attribute of the submit button is "button" and not "submit"
0
Bob LearnedCommented:
What does the <button> do?

Bob
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

raheelasadkhanAuthor Commented:
@TheLearnedOne: You answered another one of my questions a while back on how to connect to external running instances of IE. I ended up using ShellWindows.

This time I am using the WebBrowser control to display HTML content including a form. The HTML is picked up from a remote server but the data entered into the form must be stored on the local machine only. That is why I want to trap the ONCLICK events for form buttons.

I have even done something similar in the past but simply cannot remember how. There was a simple way to trap these events via MSHTML.
0
raheelasadkhanAuthor Commented:
By "button" i meant the TYPE="button" attribute. Here is an example:

<HTML>
   <BODY>
      <FORM ID="frmMain">
         Name:&nbsp;<Input ID="txtName" TYPE="text" />
         <BR />
         Email:&nbsp;<Input ID="txtEmail" TYPE="text" />
         <BR />
         <INPUT ID="btnSubmit" TYPE="button" VALUE="Submit" />
      </FORM>
   </BODY>
</HTML>
0
raheelasadkhanAuthor Commented:
From what I know, any event that can be trapped from JavaScript can also be trapped externally since these events are provided via the DOM.

So I am looking for an equivalent of the following:

<SCRIPT LANGUAGE="javascript">
   function SomeFunction()
   {
      alert ("SomeFunction called");
   }
</SCRIPT>
<INPUT ID="btnSubmit" TYPE="button" VALUE="Submit" ONCLICK="javascript:SomeFunction();"/>

My code would trap this event in C# instead of JavaScript.
0
Bob LearnedCommented:
If you are using a WebBrowser control, and using a <Submit> button, the page does a post-back.  The question is, "Does the WebBrowser control's DocumentComplete event fire for a post-back?"

Bob
0
raheelasadkhanAuthor Commented:
Well, if a postback occurs successfully, the DocumentComplete event is fired but I am not using a button that will cause a postback. The reason I have my button's Type attribute set to "button" instead of "submit" is so that the click event does NOT cause a postback. I simply want to trap that event in the host application.
0
Bob LearnedCommented:
Here's a thought:

How To Handle Document Events in a Visual Basic .NET Application
http://support.microsoft.com/?kbid=311284

Mshtml.HTMLDocumentEvents2_Event

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
raheelasadkhanAuthor Commented:
That looks just about what I need. I'll try it in C# and get back to you. Thanks.
0
raheelasadkhanAuthor Commented:
@TheLearnedOne:

Thanks for the link. After reading the article, the C# translation was very simple:
((mshtml.HTMLDocumentEvents2_Event) htmlDocument).onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(this.Main_onclick);
private bool Main_onclick(mshtml.IHTMLEventObj pEvtObj) {}

To achieve the result I wanted, I used the HTMLElementEvents2_Event instead of HTMLDocumentEvents2_Event:
((mshtml.HTMLElementEvents2_Event) htmlDocument).onclick +=new mshtml.HTMLElementEvents2_onclickEventHandler(this.Main_onclick);
private bool Main_onclick(mshtml.IHTMLEventObj pEvtObj) {}

Thanks again.
0
raheelasadkhanAuthor Commented:
Beware though. I used this and other methods within the WebBrowser1_NavigateComplete2 event and it so happens that the browser has not finished rendering the page at this time and will not even start until this trapped method returns. I had to use a thread or timer with a while (WebBrowser1.Busy) loop to get around it.
0
raheelasadkhanAuthor Commented:
Sorry. Spoke too soon.

The following code is an invalid cast: htmlElementEvents2_Event = (mshtml.HTMLElementEvents2_Event) htmlElement;
So does this version: htmlElementEvents2_Event = (mshtml.HTMLElementEvents2_Event) htmlElement.onclick;




mshtml.HTMLButtonElement            htmlButtonElement            = null;
mshtml.IHTMLElement            htmlElement            = null;
mshtml.HTMLDocument            htmlDocument            = null;
mshtml.HTMLElementEvents2_Event      htmlElementEvents2_Event      = null;

if (this.wbAdministrator.Document != null)
{
   htmlDocument = (mshtml.HTMLDocument) this.wbAdministrator.Document;
   htmlElement = htmlDocument.getElementById("btnQuoteSubmit");

   if (htmlElement != null)
   {
      if (htmlElement is mshtml.HTMLButtonElement)
      {
         htmlButtonElement = (mshtml.HTMLButtonElement) htmlElement;
         htmlElementEvents2_Event = (mshtml.HTMLElementEvents2_Event) htmlElement;
         htmlElementEvents2_Event.onclick += new mshtml.HTMLElementEvents2_onclickEventHandler(htmlElementEvents2_Event_onclick);
      }
   }
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.