从类库调用Javascript [英] Calling Javascript from a Class Library

查看:175
本文介绍了从类库调用Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个类库DLL,从任何第三方应用程序引用,它只包含一个函数,调用JavaScript来读取本地文件,并从文件返回一些值到引用应用程序。

I have created a class library DLL to be referenced from any third-party application and it contains only one function that calls a JavaScript to read a local file and returns some values from the file to referencing application.


  1. 我使用过: System.Web.HttpContext.Current.Response.Write

    ,但它在引用页面的起始位置写入JavaScript函数,因此无法执行。

  1. I used: System.Web.HttpContext.Current.Response.Write
    but it writes the JavaScript function at the beginning of the referencing page so it can never be executed.

然后,在我使用的引用页面的结尾:
Dim CSM As UI.ClientScriptManager = System.Web.UI.Page.ClientScript

和I也使用:

Then, to write the JavaScript at the end of the referencing page I used: Dim CSM As UI.ClientScriptManager = System.Web.UI.Page.ClientScript
and I used also:

Me.Page.ClientScript
CSM.RegisterClientScriptBlock(Me.GetType(),SCRIPTNAME,JavaScriptSuntax.ToString )

它显示一条错误消息:对非共享成员的引用需要共享引用。

And it shows an error message: Reference to a non-shared member requires a shared reference.

我试过: ScriptManager.RegisterStartupScript(,Me.GetType(), SCRIPTNAME,JavaScriptSuntax.ToString)

但它给了我一个错误消息:名称ScriptManager未声明。

I tried: ScriptManager.RegisterStartupScript("", Me.GetType(), "SCRIPTNAME", JavaScriptSuntax.ToString)
but it gave me an error message: Name "ScriptManager" is not declared.

我添加以下内容:

System.Web,System.Web.UI, System.Web.UI.ClientScriptManager,System.Web.UI.Page,System.Text

如何从类库DLL调用JavaScript可以从任何引用的asp.net应用程序中正确执行?

How can I call a JavaScript from a class library DLL to be performed correctly from any referencing asp.net application??

感谢您的帮助。

代码示例:

** Correction now it writes the JavaScript in the body tag but for some reason it doesn't work!!!  

'Function in Class Library DLL  
Function ReadClientFile() As Boolean  
Try  
Dim JavaScriptSuntax As StringBuilder = New StringBuilder()  
JavaScriptSuntax.Append(" var FSO = new ActiveXObject('Scripting.FileSystemObject');")  
JavaScriptSuntax.Append(" var nForReading=1;")  
JavaScriptSuntax.Append(" var fileLines;")  
JavaScriptSuntax.Append(" var OldKeyLine;")  
JavaScriptSuntax.Append(" var NewKeyLine;")  
JavaScriptSuntax.Append(" var oFileObj = FSO.OpenTextFile('D:\TestJScript.txt',nForReading, false);")
JavaScriptSuntax.Append(" var sFileContents=oFileObj.ReadAll();")  
JavaScriptSuntax.Append(" fileLines = sFileContents.split('\n');")  
JavaScriptSuntax.Append(" for(var intMissed = 0; intMissed < fileLines.length; intMissed++)")  
JavaScriptSuntax.Append(" {")  
JavaScriptSuntax.Append(" var myRegExp = /Doc_|_New/;")  
JavaScriptSuntax.Append(" var string1 = fileLines[intMissed];")  
JavaScriptSuntax.Append(" var matchPos1 = string1.search(myRegExp);")  
JavaScriptSuntax.Append(" if(matchPos1 != -1)")  
JavaScriptSuntax.Append(" {")  
JavaScriptSuntax.Append(" NewKeyLine = sFileContents.split(' = ');")  
JavaScriptSuntax.Append(" if(NewKeyLine[1].trim == '')")  
JavaScriptSuntax.Append(" {")  
JavaScriptSuntax.Append(" alert('Doc Key has not been updated!');")  
JavaScriptSuntax.Append(" }")  
JavaScriptSuntax.Append(" Else")  
JavaScriptSuntax.Append(" {")  
JavaScriptSuntax.Append(" alert('Doc Key has been updated and the NewKey= ' + NewKeyLine[1]);")  
JavaScriptSuntax.Append(" }")  
JavaScriptSuntax.Append("}")  
JavaScriptSuntax.Append(" else")  
JavaScriptSuntax.Append(" {")  
JavaScriptSuntax.Append(" }")  
JavaScriptSuntax.Append(" }")  
JavaScriptSuntax.Append(" oFileObj.Close();")  

Dim page As Page = HttpContext.Current.Handler  
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "SCRIPTNAME", JavaScriptSuntax.ToString, True)  
Return True  
Catch ex As Exception  
   gstrErrorMsg = ex.Message  
   Return False  
End Try  
End Function  


' Button Click Function in referencing ASP.NET Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim IsDone As Boolean = DispCaller. ReadClientFile()
End Sub


推荐答案

HttpContext 中获取页面实例,如下所示:

You can get the page instance from within the HttpContext like this:

Page page = (Page)(HttpContext.Current.Handler);
page.ClientScript.RegisterClientScriptBlock(...);

这是C#,但应该很容易转换为 VB.NET

This is C# but should be easy to convert to VB.NET as well.

编辑:这里是VB语法:

here is the VB syntax:

Dim page As Page = HttpContext.Current.Handler
page.ClientScript.RegisterClientScriptBlock(...)

这篇关于从类库调用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆