mshtml fireevent onchange not firing

1.5k Views Asked by At

I am unable to fire an "onchange" event in mshtml. Can you please tell me what I am doing wrong here.

HTMLSelectElement element = (HTMLSelectElement)this.HTMLDocument.all.item(controlId, 0);
IHTMLElement e = element as IHTMLElement;
IHTMLDocument4 doc = e.document as IHTMLDocument4;
object dummy = null;
object eventObj = doc.CreateEventObject(ref dummy);
HTMLSelectElementClass se = element as HTMLSelectElementClass;
se.FireEvent("onchange", ref eventObj);

I am getting variable "se" as null. I got this piece of code from another link http://www.itwriting.com/phorum/read.php?3,1507

Can anyone help me with this.

Thanks, Sam

2

There are 2 best solutions below

0
On BEST ANSWER

I had tried all those which Sheng mentioned but didn't work.

This issue was solved by injecting javascript code for "onchange" and executing it. It worked.

0
On

Runtime Callable Wrapper objects generated by COM calls like HTMLDocument.all.item can translate interface casting to QueryInterface calls. But the RCW does not know how to convert to a managed class like HTMLSelectElementClass, thus it returns null.

Instead of casting to HTMLSelectElementClass, cast to IHTMLElement3 to call fireEvent.

By the way, your code does not work in IE11 mode as document.all is deprecated. Use IHTMLDocument3::getElementById instead.