Search code examples
c#asp.netresponse.redirecttargets

Redirecting new tab on button click.(Response.Redirect) in asp.net C#


I'm trying to open a page in new tab/window on button click.I tried in the google got this code but its not working.

Can anybody help me with this?

<asp:Button ID="btn" runat="Server" Text="SUBMIT" 
     OnClick="btnNewEntry_Click" OnClientClick="aspnetForm.target ='_blank';"/>

protected void btnNewEntry_Click(object sender, EventArgs e)
{
    Response.Redirect("CMS_1.aspx");
}

When I use this I'm getting error saying

   Microsoft JScript runtime error: 'aspnetForm' is undefined.

Solution

  • I think your code should work just remove one thing from here but it'll do redirection from current page within existing window

    <asp:Button ID="btn" runat="Server" Text="SUBMIT" 
    
     OnClick="btnNewEntry_Click"/>    
    
    
    
    protected void btnNewEntry_Click(object sender, EventArgs e)
     {
        Response.Redirect("CMS_1.aspx");
     }
    

    And if u wanna do the this via client side scripting Use this way

    <asp:Button ID="BTN" runat="server" Text="Submit" OnClientClick="window.open('Default2.aspx')" />
    

    According to me you should prefer the Client Side Scripting because just to open a new window server side will take a post back and that will be useless..