[c#] How to open new browser window on button click event?

How to open new browser window on button click event in C# ASP.NET?

Please share any example.

I am doing following code. Please let me know where I am going wrong.

btn_Click()
{
    if(condition==true)
    {
        this.Page.ClientScript.RegisterStartupScript(
            this.GetType(),
            "page_index_script2", 
            "openNewWindow();", 
            true
        );
    }
}

And the JavaScript function is

function openNewWindow()
{
    alert('HI');
    window.open('http://www.stackoverflow.com');  
}

When I run the code from javascript function Alert works but new window is not getting opened.

This question is related to c# asp.net

The answer is


Response.Write('... javascript that opens a window...')

http://www.aspspider.com/qa/Question2714.aspx


It can be done all on the client-side using the OnClientClick[MSDN] event handler and window.open[MDN]:

<asp:Button 
     runat="server" 
     OnClientClick="window.open('http://www.stackoverflow.com'); return false;">
     Open a new window!
</asp:Button>

Or write to the response stream:

Response.Write("<script>");
Response.Write("window.open('page.html','_blank')");
Response.Write("</script>");