C# .NET - Button click to open in new tab?

Asked By Ananth prasath on 04-Jul-11 07:06 AM
Hi..

Am using asp.net.
How to open the new tab at the time of button click? using c#.net

Thanks in Advance
Riley K replied to Ananth prasath on 04-Jul-11 07:19 AM
I don't think it can be controlled with button click,

Using the browser you can specify how to open

from the browser settings you have to select when new page in opened
wheterh to open in new tab or new browser window

which browser r u using??
Jitendra Faye replied to Ananth prasath on 04-Jul-11 07:20 AM
I think you want to open new window in button click-

Follow this code-

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

Ananth prasath replied to Jitendra Faye on 04-Jul-11 07:24 AM
I Think the following code will work

<form id="form1" runat="server">

<div>
<asp:Button runat="server" ID="btn1" Text="submit" onclick="btn1_Click" OnClientClick="form1.target='_blank';" />
</div>
</form>
Ananth prasath replied to Riley K on 04-Jul-11 07:25 AM
Am not asking about client side. am asking about server side.
DL M replied to Ananth prasath on 04-Jul-11 07:25 AM
Try this  code open new tab/window.

<asp:Button ID=”btnNewEntry” runat=”Server” CssClass=”button” Text=”New Entry” OnClick=”btnNewEntry_Click” OnClientClick=”aspnetForm.target =’_blank’;”/>

<%– aspnetForm.target =’_blank’ will add handler to open new tab–%>

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

OR

Response.Write( “<script> window.open( ‘pageName.aspx’ ); </script>”);
Response.End();

// Here we can write javascript code to open new tab

dipa ahuja replied to Ananth prasath on 04-Jul-11 07:25 AM
Try this
protected void btn1_Click(object sender, EventArgs e)
  {
    Response.Write("<script>window.open('flv.aspx')</script>");
  }
Ananth prasath replied to DL M on 04-Jul-11 07:26 AM
Thanks for your reply.
Ananth prasath replied to dipa ahuja on 04-Jul-11 07:26 AM
Thanks for your reply.
Ravi S replied to Ananth prasath on 04-Jul-11 07:27 AM
HI

try this

<form id="form1" runat="server">

<div>
<asp:Button runat="server" ID="btn1" Text="submit" onclick="btn1_Click" OnClientClick="form1.target='_blank';" />
</div>
</form>

(or)

use  window.open('.aspx page name')
Ananth prasath replied to Ravi S on 04-Jul-11 07:29 AM
yeah i got the solution

<asp:Button ID=”btnNewEntry” runat=”Server” CssClass=”button” Text=”New Entry” OnClick=”btnNewEntry_Click” OnClientClick=”aspnetForm.target =’_blank’;”/>

<%– aspnetForm.target =’_blank’ will add handler to open new tab–%>

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

OR

Response.Write( “<script> window.open( ‘pageName.aspx’ ); </script>”);
Response.End();

Jitendra Faye replied to Ananth prasath on 04-Jul-11 07:30 AM

For Server side use this code-

<asp:Button ID=”BtnOpenNew” runat=”Server”  Text=”Open Window” OnClick=”BtnOpenNew_Click” >


protected void BtnOpenNew_Click(object sender, EventArgs e)
{
   Response.Redirect(”NewPage.aspx”);
}

Try and let me know.

Ravi S replied to Ananth prasath on 04-Jul-11 07:30 AM
HI

try this

<asp:Button ID=”btnNewEntry” runat=”Server” CssClass=”button” Text=”New Entry” OnClick=”btnNewEntry_Click” OnClientClick=”aspnetForm.target =’_blank’;”/>

<%– aspnetForm.target =’_blank’ will add handler to open new tab–%>

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


(OR)

Response.Write( “<script> window.open( ‘pageName.aspx’ ); </script>”);
Response.End();

Ananth prasath replied to Jitendra Faye on 04-Jul-11 07:31 AM
Yeah i got the solution

The below code give me the solution

<asp:Button ID=”btnNewEntry” runat=”Server” CssClass=”button” Text=”New Entry” OnClick=”btnNewEntry_Click” OnClientClick=”aspnetForm.target =’_blank’;”/>

<%– aspnetForm.target =’_blank’ will add handler to open new tab–%>

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

OR

Response.Write( “<script> window.open( ‘pageName.aspx’ ); </script>”);
Response.End();

Bala Marish replied to Ananth prasath on 04-Jul-11 08:03 AM


Use this script

<script language="javascript">
function NewWindow(para)
{
  //var location = "../Admin_Tutorials_view.aspx?id="+para;
  // window.open(location,"zoom window", "height=500,width=500");
  window.open("Admin_Tutorials_Newtab.aspx?Id="+para, "Window1","menubar=no,width=830,height=560,toolbar=no,scrollbar=true");

}
</script>

Riley K replied to Ananth prasath on 04-Jul-11 08:03 AM
First of all you have to be clear with your Question, first you said you wanted in a new tab, now in new window

if you want at server side use this code

protected void btnOpenPop_Click(object sender, EventArgs e)

    {


        string url = "http://www.xyz.com";


        ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");


    }

the JS function

<script language="javascript" type="text/javascript">


        function openNewWin(url) {


            var x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');


            x.focus();


        }


</script>


paul z replied to Riley K on 04-Jul-11 03:48 PM
target='_blank'     is your friend. But you should also setup in the browser to enable new window opened in a new tab.
Devil Scorpio replied to Ananth prasath on 04-Jul-11 04:19 PM
Hi,

Here is a demo code snippet, copy-paste into a plain text file with .htm extension and open into browser. Click on the button and it should open www.google.com in a new tab.

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title></title>
<script type="text/javascript">
function OpenNewPage() {
window.open('http://www.itfunda.com', "", "");
}
</script>
</head>
<body>
<input type="button" name="btn" id="btn" value="Click to open page in new tab" onclick="OpenNewPage()" />
</body>
</html>
Here we have a button, on click of the button we are calling OpenNewPage javascript function. In that function, we are using the window.open method by passing the url to open the web page.
Kirtan Patel replied to Ananth prasath on 04-Jul-11 10:10 PM
Here is code to Open New tab on button click

  Untitled document
protected void Button1_Click(object sender, EventArgs e)
{
   string queryString
    = "http://localhost:39208/TreeView.aspx?param1="+ TextBox1.Text.Trim();
   string newWin = "window.open('" + queryString + "');";
   ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true);
}