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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users


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.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

630 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