4

Summary: I have a GridView with a TemplateField containing only an ASP.NET LinkButton. My LinkButton looks like this:

<asp:LinkButton ID="lbDoc" runat="server" OnClientClick="document.forms[0].target ='_blank';" OnCommand="ProcessRequest" Text='<%# Eval("Title").ToString() %>' CommandArgument='<%# Eval("ID").ToString() %>' />

Note the CommandArgument and the OnCommand property of the control. Each button needs to perform some server-side processing, dependent upon the CommandArgument, and ultimately redirect and present a PDF in the new window. IMPORTANT: I am not just linking to the PDF URL so I cannot use a HyperLink control or a simple anchor tag. The server-side processing is key and required.

The problem: At run-time, once a user clicks on one of the LinkButtons, the PDF is loaded as desired in a new window, however all postbacks after that also load in a new browser tab/window. I've got DropDownLists (with AutoPostback) that requery and rebind my GridView - if the user selects different filter options in those DropDownLists after clicking on one of the LinkButtons, the results are posted back in a new browser tab/window. That's the problem - the user will end up with a mess of tabs/windows.

What I've Tried User Pow-Ian described the same problem here, but the proposed Javascript solution did not work for me. I've tried a pure JavaScript solution like this, but it also doesn't work. It will render the target='_blank' on the resulting tag, but for some reason, it won't process the link in a new browser tab.

Any suggestions?

Community
  • 1
  • 1
lunchroom
  • 41
  • 4
  • You mentioned you can't use anchor tag, I am a bit unclear on that, seems that you only rely on the value from commandArgument to generate pdf link url, so why the anchor can't make it? it could be something like this: " target="_blank">Loading in new window, the logic in the event handler ProcessRequest could be ported over to pdf.aspx. did I miss anything? – Kane Wang Jun 25 '15 at 15:46
  • Kane - Unfortunately, I do not "only rely on the value from commandArgument to generate pdf link url". As I stated, I am not just linking to the PDF URL. Server side processing is key. I am doing several server-side operations prior to **streaming** the PDF to the browser. Additionally, I do not want a user to be able to share or bookmark the link using a querystring parm as you suggested. – lunchroom Jun 25 '15 at 16:59
  • There should be many ways to protect the URL, for example, adding a token expiring in 15 minutes as a parameter in the URL, in this way, even the URL is visible to the user, he can't simply use it later on. If all the process could be done independently from the parent page, I think it is still doable to use an anchor. – Kane Wang Jun 26 '15 at 01:06
  • Huge credit to [Atters](http://stackoverflow.com/users/2203520/atters). [This suggestion](http://stackoverflow.com/a/20236113/5046381) resolved my issue. – lunchroom Jul 01 '15 at 19:23

1 Answers1

-2

Add the following code to prevent post-backs.

lbDoc.Attributes.Add("onclick","return false;");
Elim Garak
  • 1,728
  • 1
  • 16
  • 21