Check Out Our New Venture

Tuesday, February 22, 2011

Open url in new tab using response.redirect - Asp.Net

Here I am going to explain how to open new tab/window on Button click using asp.net.
A week ago, i have to implement that code. I have option to use javascipt windows.open to open new tab.
But I have to insert or update some database entry. Yeah i know that there is another way to use clientscipt in code behind to achieve this.
But in its simplest form, following code open new tab/window.

//Old
<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


//Updated
<asp:Button ID=”btnNewEntry” runat=”Server” CssClass=”button”  Text=”New Entry” OnClick=”btnNewEntry_Click”  OnClientClick=”document.getElementById('form1').target =’_blank’;”/>

// document.getElementById('form1').target =’_blank’ will add handler to open new tab

protected void btnNewEntry_Click(object sender, EventArgs e)
{
Response.Redirect(”New.aspx”);
}
aspnetForm is the form name, it can be different in some pages, so be sure to check the viewsource of the page you are working on.
Cheers

14 comments:

John said...

Thanks for the info. Worked for me.

Anonymous said...

you rock it really2x works thank you so much!!!!

from: http://infotechnolozyx.blogspot.com/

Anonymous said...

not working for me

Anonymous said...

not working for me

Anonymous said...

slight change might work for every one
replace
OnClientClick=”aspnetForm.target =’_blank’;”
with
OnClientClick="document.getElementById('form1').target ='_blank';"

Anonymous said...

"form1" is the id of the form tag

Anonymous said...

OnClientClick="document.getElementById('form1').target ='_blank';"

Works for me ! Thanks !

Jeyachandran said...

It works.. But not only for the particular control.. If i click some other controls in the same page, after click this control, That control also open a new tab.. Plz check that..

Anonymous said...

OnClientClick="document.getElementById('form1').target ='_blank';"

Works for me ! Thanks !

Anonymous said...

working fine., thanx

Anonymous said...




its working

Anonymous said...

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

Anonymous said...

Thanks Bro. Its Working Fine

hari said...

working..thanks a lot man!! :)

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More