我创建了一个类库DLL,可以从任何第三方应用程序引用它,它只包含一个调用JavaScript来读取本地文件的函数,并从文件返回一些值到引用应用程序。
我用过:System.Web.HttpContext.Current.Response.Write
但它会在引用页面的开头写入JavaScript函数,因此永远无法执行。
然后,在我使用的引用页面末尾编写JavaScript:
Dim CSM As UI.ClientScriptManager = System.Web.UI.Page.ClientScript
我还用过:
Me.Page.ClientScript
CSM.RegisterClientScriptBlock(Me.GetType(), "SCRIPTNAME", JavaScriptSuntax.ToString)
它显示错误消息:对非共享成员的引用需要共享引用。
我试过了:ScriptManager.RegisterStartupScript("", Me.GetType(), "SCRIPTNAME", JavaScriptSuntax.ToString)
但它给了我一条错误消息:名称“ScriptManager”未声明。
我添加了对以下内容的引用:
System.Web,System.Web.UI,System.Web.UI.ClientScriptManager,System.Web.UI.Page,System.Text
如何从任何引用的asp.net应用程序中正确执行类库DLL中的JavaScript?
感谢您的帮助。
代码示例:
** 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
答案 0 :(得分:1)
您可以从HttpContext
中获取页面实例,如下所示:
Page page = (Page)(HttpContext.Current.Handler);
page.ClientScript.RegisterClientScriptBlock(...);
这是C#,但也很容易转换为VB.NET
。
编辑:这是VB语法:
Dim page As Page = HttpContext.Current.Handler
page.ClientScript.RegisterClientScriptBlock(...)
答案 1 :(得分:1)
您可以添加System.Web.Extensions
&amp;的引用System.Web.UI class
到您的类库并获得使用ScriptManager的权限。 RegisterClientStartUp函数也可以在Update Panel中调用。