当前位置:编程学习 > C#/ASP.NET >>

ActiveX 调用 javascript失败


<script event="OnReadTraceCode(traceCode)" for="TraceCodeActiveX" type="text/javascript"> 
 document.location.href = "MilkTraceInfo.aspx#" + traceCode;</script> 


<object id="TraceCodeActiveX" name="TraceCodeActiveX" classid="clsid:3EAE5741-8A27-41EA-BACB-99AC5F87B21E" width="520" height="240" codebase="MISCode/MilkPowderTrace.msi"></object>



[Guid("3EAE5741-8A27-41EA-BACB-99AC5F87B21E")]
    [ClassInte易做图ce(ClassInte易做图ceType.AutoDual), ComSourceInte易做图ces(typeof(ControlEvents))]
    [ComVisible(true)]
    public partial class TraceCodeActiveX : UserControl, IObjectSafety

 public delegate void ControlEventHandler(string TraceCode);


        public event ControlEventHandler OnReadTraceCode;


        [ComVisible(true)]
        public void ReadTraceCode(string TraceCode)
        {
            if (OnReadTraceCode != null)
            {
                OnReadTraceCode(TraceCode);
            }
            else
            {
                MessageBox.Show("未正确加载方法");
            }
        }


        [Guid("53B97E22-18F9-4952-9E89-C5C6503CF2E0")]
        [ComVisible(true)]
        [Inte易做图ceType(ComInte易做图ceType.Inte易做图ceIsIDispatch)]
        public inte易做图ce ControlEvents
        {
            [DispId(0)]
            void ReadTraceCode(string TraceCode);
        }


        private void labTraceCode_Click(object sender, EventArgs e)
        {
            ReadTraceCode("23453253424");
        }

一直提示“未正确加载方法”

好像是没加正常加载javascript里的方法
--------------------编程问答-------------------- 想在ActiveX中调用javascript

 //IHTMLWindow2 在C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies

\Microsoft.mshtml.dll

首先由js调用activex控件方法,目的是把html的window窗体传递给activex控件,进而获得

IHTMLWindow2对象,然后activex控件根据获得IHTMLWindow2对象再调用js函数。
javascript里这这样的 document.getElementById('TraceCodeActiveX').SetHtmlWin2(window);

控件里的代码

 public void SetHtmlWin2(object htmlWin2)//对应javascript
        {
            HtmlWin2 = htmlWin2 as IHTMLWindow2;
        }

 public void CallJSFunction(string JSFuncName)
         {
             if (htmlWin == null || string.IsNullOrEmpty(JSFuncName))
             {
                 MessageBox.Show("错误");
             }
             else
             {                
                 //调用js代码1,反射的方法;2,JS代码语法
                //方法1。 
                 htmlWin.GetType().InvokeMember(JSFuncName,
                    BindingFlags.Instance | BindingFlags.InvokeMethod | 

BindingFlags.Public,
                    null, htmlWin, new object[] { "C#参数" });
                 //方法2。 
                 string jsCode = string.Format("{0}('{1}')", JSFuncName, "C#参数");
                 htmlWin.execScript(jsCode, "JScript");
             }
         }//JSFuncName就是方法名,两方式都可以,我认为用第二个方便些


页面中的javascript方法完整的代码
<script type="text/javascript"> 
  function SetHtmlWin2ToActiveX()
        {
            document.getElementById('ActiveX5').SetHtmlWin2(window);
        }

 function ReadCode(traceCode)
        {
            document.location.href = "MilkTraceInfo.aspx#" + traceCode;
        }

        SetHtmlWin2ToActiveX();//最好是网页加载完在执行,所以我放在了object标签后面

 </script>
补充:.NET技术 ,  组件/控件开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,