Open aspx page in new window without popup

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
Who is Participating?
Muhammad Ousama GhazaliConnect With a Mentor Solution Analyst & ArchitectCommented:
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.
Muhammad Ousama GhazaliSolution Analyst & ArchitectCommented:
Replace Hyperlink with LinkButton and you'll have the click event.
You can attach the client-side javascript for a control as below:

BtnSubmit.Attributes.Add("onClick", "''); return false;")
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

Muhammad Ousama GhazaliSolution Analyst & ArchitectCommented:
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

jeovaalmeidaAuthor Commented:
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.
jeovaalmeidaAuthor Commented:
response to comment from pendem:
I tried your suggestion, but the click event from my code is not triggered. (just the javascript one)
jeovaalmeidaAuthor Commented:
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.
jeovaalmeidaAuthor Commented:
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.
jeovaalmeidaAuthor Commented:
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?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.