Open aspx page in new window without popup

Posted on 2009-05-15
Last Modified: 2012-05-07
When I use a Hypelink control, I can set the target property to '_blank', and the page is opened in new window (IE) or tab (firefox).

I need this same funcionality, but I need that a click event be processed, too (code behind). The problem is that this control doesn't have a click event.

My page uses ajax. I managed to open the page in new window using ScriptManager.RegisterClientScriptBlock(), but it opens the new window as a popup window, being blocked by the browsers.

How can it be done? Is there a better solution than Hyperlink control?

ps. visual studio 2008
Question by:jeovaalmeida
  • 5
  • 3
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24395801
Replace Hyperlink with LinkButton and you'll have the click event.

Expert Comment

ID: 24395832
You can attach the client-side javascript for a control as below:

BtnSubmit.Attributes.Add("onClick", "''); return false;")
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24396134
Sorry I intepretted your question wrongly when I suggested the LinkButton above. Here is a trick that you may follow to acheive what you are trying to do:
  1. Create the HyperLink as you are doing right now with Target set to _blank.
  2. Create a button and name it anything (this will be hidden always) and is used for processing an event on the page.
  3. Use the sample code snippet below.
I have tested it on my machine in VS 2008/.NET 3.5 SP1 but without UpdatePanel.

<%--ASPX Page--%>

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/TargetPage.aspx" Target="_blank">HyperLink with hidden button event</asp:HyperLink>

<asp:Button ID="btnHidden" runat="server" Text="Button" />

'Codebehind file 

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

	btnHidden.Attributes.Add("style", "display:none;")

	HyperLink1.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(btnHidden, Nothing) & ";")

End Sub

'Use this routine to process the postback from JavaScript initiated postback click event

Private Sub btnHidden_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHidden.Click

End Sub

Open in new window


Author Comment

ID: 24396181
response to comment from moghazali:
I can't do that because I need the page to be opened in new window or tab.
The hyperlink control, as I said before, has the target property which does what I need. The LynkButton has click event but not target propety.
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!


Author Comment

ID: 24396210
response to comment from pendem:
I tried your suggestion, but the click event from my code is not triggered. (just the javascript one)

Author Comment

ID: 24396775
hello moghazali,
we're almost there. it worked, but it seems that javascript part, the onclick that uses
ClientScript.GetPostBackEventReference(Button1, null) is run before button1_click() event.
it's primordial that the button1_click() is run before the javascript onclick, because the button1_click prepare info and save in the session, so the new opened page uses that, or it will fail.
LVL 11

Accepted Solution

Muhammad Ousama Ghazali earned 500 total points
ID: 24396927
First, the javascript essentially is calling the postback to be initiated with Button1's signatures so that we would have its click event fired at code behind.
Now, I just tested at my machine with and without debug mode and the code is Button1_Click event is fired, a session variable is set which then is printed in the target page within the same application and it has the value from Session correctly. So your requirements are met.

Author Comment

ID: 24398624
Ii checked my code and found a bug. Indeed, it's working perfecty.
Thanks Moghazali.
To complete the information about this issue, when I run my real web site with this solution, occurs the error:
Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
I didn't wnt to go for <%@ Page EnableEventValidation="true" %>, so I used ClientScriptManager.RegisterForEventValidation():

protected override void Render(HtmlTextWriter writer)
   string s = btGerarRelatorio.ID;
That solved the last error.

Author Comment

ID: 24398665
I'm a little confused about finishing this question. I want to accept the solution from Moghazali and award points.
Should I click the'Accept and Award Points' button from my last post? And if there was replies from many experts? Which one would accept the points?
Or should I click 'Accept as Solution' at the Moghazali comment that exposed the code that I used as solution?

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now