gpt4 book ai didi

javascript - 从类库调用 Javascript

转载 作者:行者123 更新时间:2023-11-29 22:35:13 25 4
gpt4 key购买 nike

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

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

  2. 然后,在我使用的引用页面末尾编写 JavaScript:Dim CSM As 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、System.Web.UI、System.Web.UI.ClientScriptManager、System.Web.UI.Page、System.Text

如何从类库 DLL 中调用 JavaScript,以便从任何引用的 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

最佳答案

您可以像这样从 HttpContext 中获取页面实例:

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

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

编辑:这里是 VB 语法:

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

关于javascript - 从类库调用 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5367946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com