ASP.Net VB call JavaScript function from Code Behind
Asked Answered
P

4

6

I have a function that is responsible for populating an SSRS report. The user presses a button and I go out and check to see if there is data. If no data, I provide a NO DATA message. If there is data I call the SSRS report which I would like to open in a new window. I thought using a JavaScript function would be the best way to accomplish this. How is this done or what would you recommend? Thanks in advance!

    <script type="text/javascript">
        function openWindow(url) {
            document.forms[0].target = "_blank";
        }
    </script>
Porker answered 10/2, 2012 at 21:25 Comment(0)
C
12

Try this:

 System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "Script", "openWindow(url);", True)
Cherimoya answered 11/2, 2012 at 0:2 Comment(0)
A
5

Please consider using ClientScriptManager.RegisterClientScriptBlock method. It's a static method and should be avaible on your page.

Example of usage is following:

Page.ClientScript.RegisterStartupScript(Me.GetType(), "window-script", "openWindow('someUrl')", True)

Of course you can put any url that you have prepared in your code behind...

Hope it helps.

Arnettearney answered 10/2, 2012 at 21:28 Comment(0)
E
4

Something to keep in mind here: while your vb code is running, the page in your browser doesn't exist.

Remember that the web works via the http protocol, and the http protocol boils down to requests and responses. Always a request first, followed by a response. Often, the response is html, and this is where your vb code comes in. The sole purpose of all the code and aspx markup is so that your web server can send html back to the browser. There is no active connection between the browser and your server. While your vb code is running, the web page in your browser doesn't really exist (your code is busy creating it). While your javascript is running, and vb resources used to create it on the server have long since been recycled.

Extravagance answered 11/2, 2012 at 0:14 Comment(0)
T
1

If you are trying to execute javascript on a page depending on some results of server side logic, one way you could achieve this is by using asp literal controls (or other types of asp controls)

Example:

<asp:Literal ID="literalJavascript" runat="server">
    function openWindow() { ... }

    openWindow();
</asp:Literal>

Then just show or hide this literal in your server code based on your other logic.

Traceable answered 10/2, 2012 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.