Open new tab on LinkButton Click using C# and VB.Net in ASP.Net

Category > ASP.NET || Published on : Monday, October 14, 2019 || Views: 168 || Open new tab on LinkButton Click


Introduction

Here Pawan Kumar will explain how to Open new tab on LinkButton Click using C# and VB.Net in ASP.Net

Description

Now I will explain How to Open new tab on LinkButton Click using C# and VB.Net in ASP.Net

So follow the steps to learn Open new tab on LinkButton Click using C# and VB.Net in ASP.Net

Step 1 - Create a asp.net website using Visual Studio 2019 por any other version


Step 2 - Add a new Webform C# into the website created in step 1


Step 3 - Drag and drop link button from visual studio toolbar.


Step 4 -  CS.ASPX codes are below

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="examples_1_CS" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:LinkButton Text="Open" runat="server" ID="lnk" OnClick="lnk_Click" />
        </div>
    </form>
</body>
</html>

Step 5: In the code behind file of CS.ASPX add the following codes


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class examples_1_CS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void lnk_Click(object sender, EventArgs e)
    {
        string Url = "DEMO.aspx"; // change with filename you want to open in new tab
        Response.Write("<script language='javascript'>window.open('" + Url + "','_blank','');");
        Response.Write("</script>");
    }
}

Visual Basic version is also available below

VB.ASPX

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="VB.aspx.vb" Inherits="examples_VB" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:LinkButton Text="Open" runat="server" ID="lnk" OnClick="lnk_Click" />
        </div>
    </form>
</body>
</html>

Vb.aspx.vb


Partial Class examples_VB
    Inherits System.Web.UI.Page

    Protected Sub lnk_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim Url As String = "Default.aspx"
        Response.Write("<script language='javascript'>window.open('" & Url & "','_blank','');")
        Response.Write("</script>")
    End Sub

End Class

In this example we have write javascript window open method with url we want to open. The codes in response method we write javascript window open codes

Conclusion:

So, In this tutorial we have learned, Open new tab on LinkButton Click using C# and VB.Net in ASP.Net