Question

Clicking on ASP.NET button redirects to correct website but on the same tab, not in a new tab what i need to do. What's wrong with the code OnClientClick="aspnetForm.target ='_blank';" below? Why it is not enough alone and what else need to be done?

The following ASP.NET code for the button control is:

<asp:Button ID="btnGenerateReport" runat="server" Text="Generate Report" 
OnClick="btnGenerate_Click" OnClientClick="aspnetForm.target ='_blank';" />
Was it helpful?

Solution 2

If you are looking out for server side code to open a new window on Button Click, then here's how to do so.

Add the following script to the section of your page

<script language="javascript" type="text/javascript">
    function openNewWin(url) {
        var x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');
        x.focus();
    }
</script>

Then add a Button Control in the following manner

<asp:Button ID="btnOpenPop" runat="server" Text="Open Pop"           
    onclick="btnOpenPop_Click" />

Finally add some code in the code behind file

protected void btnOpenPop_Click(object sender, EventArgs e)
{
    string url = "http://www.dotnetcurry.com";
    ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");
}

OTHER TIPS

I know two methods for redirecting the page to new tab in asp

1) The first method which you are already using and it works also. Make an onclientclick event of Button and on code behind of Button Click write the following code:-

button.OnClientClick = "aspnetForm.target='_blank'"; Response.Redirect("yourpage.aspx");

2)You can also use javascript

 button.Attributes.Add("onclick", "window.open('yourpage.aspx');return false;");

Both the method will redirect your page to new tab on clicking the button.

The error with your code is OnClientClick = "aspnetForm.target='_blank;'" remove the semicolon after '_blank' and it will work

You can't use target attribute on button.

You have to use javascript function window.open()

Check this:

Window open() Method

I don't post links lightly, but I found this site explains all and it has solved my problem too.

http://dotnetspidor.blogspot.co.uk/2009/01/open-new-window-in-aspnet-web-page_28.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top