How do I prevent postbacks from opening in new window after a LinkButton with target=_blank is clicked?

1.3k views Asked by At

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?

1

There are 1 answers

0
Elim Garak On

Add the following code to prevent post-backs.

lbDoc.Attributes.Add("onclick","return false;");