从类库调用 Javascript

发布于 2024-10-23 22:31:24 字数 3257 浏览 3 评论 0原文

我创建了一个可供任何第三方应用程序引用的类库 DLL,它仅包含一个函数,该函数调用 JavaScript 来读取本地文件并将文件中的一些值返回到引用应用程序。

  1. 我使用:System.Web.HttpContext.Current.Response.Write
    但它在引用页面的开头写入 JavaScript 函数,因此永远无法执行。

  2. 然后,为了在引用页面的末尾编写 JavaScript,我使用了: 将 CSM 调暗为 UI.ClientScriptManager = System.Web.UI.Page.ClientScript
    我还使用了:

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

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

  3. 我尝试过:ScriptManager.RegisterStartupScript("", Me.GetType(), "SCRIPTNAME", JavaScriptSuntax.ToString)
    但它给了我一条错误消息:名称“ScriptManager”未声明。

我添加了对以下内容的引用:

、System.Web.UI.Page、System.Text

System.Web、System.Web.UI、 System.Web.UI.ClientScriptManager 类库 DLL 可以从任何引用的 ASP.NET 应用程序中正确执行?

感谢您提前的帮助。

代码示例:

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

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

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

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

I add references to the following:

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

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

Thanks for the help in advance.

Code Sample:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南巷近海 2024-10-30 22:31:24

您可以从 HttpContext 中获取页面实例,如下所示:

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

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

编辑:这是VB语法:

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

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

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

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

Edit: here is the VB syntax:

Dim page As Page = HttpContext.Current.Handler
page.ClientScript.RegisterClientScriptBlock(...)
橘香 2024-10-30 22:31:24

您可以添加 System.Web.Extensions 的引用 & System.Web.UI class 添加到您的类库并获得在 ScriptManager 上工作的权限。 RegisterClientStartUp 函数也可以在更新面板中调用。

You can add reference of System.Web.Extensions & System.Web.UI class to your class library and get the privilege of Working on ScriptManager. RegisterClientStartUp function which can be called in Update Panel too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文