我創建了一個類庫DLL,可以從任何第三方應用程序中引用,它只包含一個調用JavaScript來讀取本地文件並將文件中的某些值返回給引用應用程序的函數。從類庫中調用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
非常感謝Shadow Wizard,這是我正在尋找的,這是如何在VB.NET中爲任何人需要它:Dim page As Page = HttpContext.Current.Handler page.ClientScript.RegisterClientScriptBlock(... ) – DesignStudio 2011-03-20 11:16:46
乾杯@Des很高興我能幫忙! :-) – 2011-03-20 11:21:11
嗨影子嚮導,錯誤消息消失,但JavaScript STILL寫在引用頁面的頂部。有沒有什麼辦法把它寫在頭標籤或頁面的主體標籤中? – DesignStudio 2011-03-20 11:50:18