Tuesday, August 5, 2014

Open a page in a new tab on button click from code behind

To open a new page in a new tab from a hyperlink is an easy thing but from a code behind is not a easy one to do. In this example I will show you how to redirect to a new page from code behind in C#.

First create a new project and add two new pages. page1.aspx and page2.aspx

Now in page1.aspx add two new buttons. In the code behind of two buttons add bellow codes.

Method 1 :
This is to open a page in pop up.


<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Open in Popup" OnClick="Button1_Click" />
    </div>
    </form>
</body>

Now in the code behind

/// <summary>
/// in pop up/// </summary>
/// <param name="sender"></param>       
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
     Page.ClientScript.RegisterStartupScript(
          this.GetType(), "OpenWindow", "window.open('2.aspx','_newtab');", true);
}

Method 2 :
This is to redirect a page in a another tab.


<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button2" runat="server" Text="Open in Other Tab"
            onclick="Button2_Click" OnClientClick="document.forms[0].target ='_blank';" />
    </div>
    </form>
</body>

And the code is simpler even.

/// <summary>
/// in other tab
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
     Response.Redirect("2.aspx");
}

Download the full source code here. Try it yours...Enjoy.... :)

5 comments:

  1. This blog is really helpful regarding all educational knowledge I earned. It covered a great area of subject which can assist a lot of needy people. Everything mentioned here is clear and very useful. Computer Graphics Programs

    ReplyDelete
  2. Thank you for sharing this information,its is very helpful to all
    click here for more information

    ReplyDelete

Popular Posts

Pageviews