how to call javascript function from codebehind to open a new window

Posted on 2009-12-24
Last Modified: 2012-05-08

how to call javascript function from codebehind to open a new window but it should not be pop up window because most of the users keep pop up blocker on

i have used one the code snippet given by your site only but it does not work pls help

In your specific case, to call the javascript function that Daryal typed out, you would want a C# codebehind method that looks specifically like this:

protected void OpenWindow(string name, int age)
      StringBuilder sbJavaScript = new StringBuilder();
      sbJavaScript.AppendLine("<script type=\"javascript\">");
      sbJavaScript.AppendLine("OpenNewWindow(\"" + name + "\", " + age.ToString() + ");");
      ClientScript.RegisterStartupScript(Page.GetType(), string.Empty, sbJavaScript.ToString());

Just make sure that you have the below JavaScript function within your html markup:

<script type="text/javascript">
function OpenNewWindow(name,age)

pls help

Question by:vedant0500
    LVL 8

    Expert Comment

    Can you use this:

    <asp:HyperLink ID="hyp" NavigateUrl="~/aboutus.aspx" runat="server" Text="click" Target="_blank"></asp:HyperLink>

    Or set the NavigateURL in your code behind.

    Have fun!

    Author Comment

    dear jtdebeer

    no i want to use this

    any other solution
    LVL 21

    Expert Comment

    ok...the problem is what even do you want to associate the click with?

    an event must call
    protected void OpenWindow(string name, int age)

    is that happening in your code?
    LVL 21

    Expert Comment

    just a question, but why can't you build the script in the .aspx page and then call it and pass the appropriate params?   I'm still confused as to why this javascript has to be created in the code behind...
    LVL 41

    Expert Comment

    can you try to modify you code like below:

    Point is to just inject the code and call method.
    Check this link:
    (Injecting Script blocks section)
    LVL 14

    Accepted Solution

    Go for it..


    This will open new window.. you can make the other setting for the popup in settings ..


    Author Closing Comment

    Solution was partially helpful

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now