Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Open aspx page in new window without popup

Posted on 2009-05-15
9
Medium Priority
?
1,340 Views
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
0
Comment
Question by:jeovaalmeida
  • 5
  • 3
9 Comments
 
LVL 11

Expert Comment

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

Expert Comment

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

BtnSubmit.Attributes.Add("onClick", "window.open('www.google.com'); return false;")
0
 
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

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:jeovaalmeida
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.
0
 

Author Comment

by:jeovaalmeida
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)
0
 

Author Comment

by:jeovaalmeida
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.
0
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 2000 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.
0
 

Author Comment

by:jeovaalmeida
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;
   Page.ClientScript.RegisterForEventValidation(btGerarRelatorio.UniqueID);
   base.Render(writer);
}
That solved the last error.
0
 

Author Comment

by:jeovaalmeida
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?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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

773 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