1

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

2 Answers 2

1

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(...)
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks so much Shadow Wizard, this what i was looking for and this is how it is done in VB.NET for anyone needs it: Dim page As Page = HttpContext.Current.Handler page.ClientScript.RegisterClientScriptBlock(...)
Hi Shadow Wizard, the error message disapeared but the javascript STILL is written at the top of the referenced page. Is there any way to write it inside the head tag or the body tag of the page??
@Des - This overload: msdn.microsoft.com/en-us/library/bahh2fef.aspx take a boolean as last argument, pass True to it and ASP.NET should add <script> tags around the code by itself and place it in proper place.
@Shadow Wizard - Still writes the JavaScript at the top of the page with the <script> tags around the code.
@Des this is really weird. Can you post some sample code causing such a thing?
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.