Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 679
Naresh Avatar asked Nov 28 '22 18:11

Naresh


2 Answers

You can do something like this :

<asp:Button ID="Button1" runat="server" Text="Button"
    onclick="Button1_Click" OnClientClick="document.forms[0].target = '_blank';" />
like image 65
satan singh Avatar answered Dec 05 '22 01:12

satan singh


Wouldn't you be better off with

<asp:HyperLink ID="HyperLink1" runat="server" 
            NavigateUrl="CMS_1.aspx" 
            Target="_blank">
    Click here
</asp:HyperLink>

Because, to replicate your desired behavior on an asp:Button, you have to call window.open on the OnClientClick event of the button which looks a lot less cleaner than the above solution. Plus asp:HyperLink is there to handle scenarios like this.

If you want to replicate this using an asp:Button, do this.

<asp:Button ID="btn" runat="Server" 
        Text="SUBMIT"
        OnClientClick="javascript:return openRequestedPopup();"/>

JavaScript function.

var windowObjectReference;

function openRequestedPopup() {
    windowObjectReference = window.open("CMS_1.aspx",
              "DescriptiveWindowName",
              "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
}
like image 38
naveen Avatar answered Dec 05 '22 00:12

naveen