0

App is .Net. I want to record clicks on a hyperlink. Easy enough. I changed it to a link button and can now execute the required server-side code to record the click. Problem is: Now I can't launch the link to a _blank target. I want to eat my cake and have it too. Server-side code and a _blank target. How do?

Sergio
  • 28,539
  • 11
  • 85
  • 132
  • Check this SO answer. http://stackoverflow.com/questions/2637087/link-button-property-to-open-in-new-tab – Ravi Y Nov 09 '12 at 15:08
  • protected void LinkButton1_Click(object sender, EventArgs e) { // Your existing code to record the click ... // ... // ... // Generate client-side code to open the link in a new window // (this assumes that you have stored the URL in a string variable // named targetURL) ClientScript.RegisterStartupScript(this.GetType(), "openLinkInNewWindow", "window.open('" + targetURL + "', '_blank');", true); – Steve Cross Nov 09 '12 at 19:25
  • That last coment worked for me. Thanks! – Steve Cross Nov 09 '12 at 19:26
  • "Post the code" means "edit your question and post the code there", not "post the code in a comment". – John Saunders Nov 14 '12 at 02:07

1 Answers1

0

You can use: ClientScriptManager.RegisterStartupScript Method (Type, String, String)

Eg.:

// Define the name and type of the client scripts on the page.
String csname1 = "PopupScript";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
StringBuilder cstext1 = new StringBuilder();
cstext1.Append("<script type=text/javascript>window.open(url, '_blank') </");
cstext1.Append("script>");
cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52