How call javascript function from VB code behind page

Discussion in 'Visual Studio' started by albanello, Apr 11, 2014.

  1. How do I call a javascript function from a code behind page. I am using VS 2008 Visual Basic?

    I have searched the web and ONLY find information on calling a JavaScript function using a buttons onclick event.

    I want to call the javascript function with just a MyJavaScriptFunction() inserted any in my VB code behind

    Is this possible........am I missing something so simple?

    Thanks in advance
    albanello
     
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

  3. What you are suggesting is the only thing I can find on the Web

    <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />

    Is there no way to just call a JavaScript function like this> MyJavaScriptFunction() ! What you are suggesting and what I find on the web is a button needs to be pressed to fire the onclick event. I just want to call my javascript function "MyJavaScriptFunction()"

    I just want to start and stop a javascript "Stop Watch" it needs to start just before I do a SQL SP and stop on return from the SQL SP the javascript Stop Watch will display the time while the SQL SP is running. I want to monitor my maintenance SQL SP while they are running. I already have javascript implemented to do the stop watch display and operation.

    albanello
     
  4. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Hi Albanello,

    I forgot where I got that piece of code from but that's how you can call any type of script in your code behind. You place the JavaScript in your presentation (layer) page and use the ClientScriptManager.RegisterStartupScript method to register it. You can then execute it in other places in your code behind (e.g. your PageLoad() method) not just during an onclick button event. I'm afraid I don't remember the exact syntax offhand. It might be Client.RegisterStartupScript instead.

    Take a look at these links:

    http://stackoverflow.com/questions/5731224/calling-javascript-function-from-codebehind
    http://forums.asp.net/t/1363655.aspx?Calling+a+Javascript+function+from+c+code+behind+file
     
    Last edited: Apr 11, 2014
    mjp likes this.
  5. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    I found some sample code that I wrote. Here is a method to build the Javascript string:
    Code:
    public static String OpenInNewBrowserWindow(String url)
    {
         StringBuilder sb = new StringBuilder();
         sb.Append("<script type = 'text/javascript'>");
         sb.Append("window.open('");
         sb.Append(url);
         sb.Append("','','width=1024,height=825')");
         sb.Append("</script>");
    
         return sb.ToString();
    }
    
    Here is code that will execute it:
    Code:
    String newWindow = BrowserUtility.OpenInNewBrowserWindow("MyCode.aspx");
    ClientScript.RegisterStartupScript(this.GetType(), "script", newWindow);
    
     
    mjp likes this.

Share This Page