[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2347
  • Last Modified:

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
0
raheelasadkhan
Asked:
raheelasadkhan
  • 9
  • 4
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now