|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
PrefaceI am not a native English speaker, and I am not heading for the Pulitzer Prize of Journalism. So, if anyone fine this “article” a bit short, get patient. My purpose here is to provide the most useful information with not too much talking. Besides, my C# English is fine enough for the VS2005 compiler :-). I hope the readers of this article will get their answer to the subject they Googled for: "Handling HTML events in .NET". BackgroundI was looking for a way to handle HTML events with the .NET Anyway, I looked for forums and threads that would give me a straight answer, but didn’t find any. So, that left me no choice but to go to the last resort: the Reflector (Luts Roeder’s .NET Reflector). What did I look for? When working with the Using the codeThe best way is to download and try the sample first. There is only one class you need to add to your project, this is the Let's start from the .NET event handler of the I chose to detach from the event once it is consumed, using the private void FormSubmitHabdler(object sender, EventArgs e)
{
MessageBox.Show("form submitted");
// show the outer html of the form
IHTMLElement form =
((HtmlEventProxy)sender).HTMLElement as IHTMLElement;
MessageBox.Show("outer html:" + form.outerHTML);
//detach the event from the element
((HtmlEventProxy)sender).Detach();
}
Now, for the code that binds the form object form = webBrowser1.Document.Forms[0].DomElement ;
HtmlEventProxy.Create("onsubmit", form, FormSubmitHabdler);
object button = webBrowser1.Document.GetElementById("button1").DomElement;
HtmlEventProxy.Create("onclick", button, ButtonClickHabdler);
First, obtain the element object from the document. Then, create a proxy with the name of the event, the DOM element, and the .NET handler. The “HtmlEventProxy” classThe class implements the I also implemented the public class HtmlEventProxy : IDisposable,IReflect
{
// Fields private EventHandler eventHandler;
private object sender;
private IReflect typeIReflectImplementation;
private IHTMLElement2 htmlElement = null;
private string eventName = null;
// private CTOR
private HtmlEventProxy(string eventName, IHTMLElement2 htmlElement,
EventHandler eventHandler)
{
this.eventName = eventName;
this.htmlElement = htmlElement;
this.sender = this;
this.eventHandler = eventHandler;
Type type = typeof(HtmlEventProxy);
this.typeIReflectImplementation = type;
}
public static HtmlEventProxy Create(string eventName,
object htmlElement, EventHandler eventHandler)
{
IHTMLElement2 elem = (IHTMLElement2)htmlElement;
HtmlEventProxy newProxy = new HtmlEventProxy(eventName,elem, eventHandler);
elem.attachEvent(eventName, newProxy); return newProxy;
}
/// detach only once (thread safe)
///
public void Detach()
{
lock (this)
{
if (this.htmlElement != null)
{
IHTMLElement2 elem = (IHTMLElement2)htmlElement;
elem.detachEvent(this.eventName, this);
this.htmlElement = null;
}
}
}
/// HtmlElemet property
///
public IHTMLElement2 HTMLElement
{
get
{
return this.htmlElement;
}
}
#region IReflect
......
object IReflect.InvokeMember(string name,BindingFlags invokeAttr, Binder binder,
object target, object[] args, ParameterModifier[] modifiers,
CultureInfo culture, string[] namedParameters)
{
if (name == "[DISPID=0]")
{
if (this.eventHandler != null)
{
this.eventHandler(this.sender, EventArgs.Empty);
}
}
return null;
}
#endregion
}
You can download the latest version of HtmlEventProxy.cs from here. Enjoy!
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||