ASP.Net VB call JavaScript function from Code Behind

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>

Answers:

Answer

Try this:

 System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "Script", "openWindow(url);", True)
Answer

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.

Answer

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.

Answer

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.