views:

2915

answers:

3

I have a LinkButton that has to postback to perform some logic.

Once it is finished, instead of loading the page back up in the browser, I want to leave it alone and pop open a new window.

So far, the best idea I've had is to put the LinkButton in an UpdatePanel, and have it render some JavaScript out when it reloads, yet I think that is totally hacky. Also, if I recall right, JavaScript within a update panel won't run anyways.

Any other ideas?

+1  A: 

One thing you could try is to have your LinkButton OnClick event do its processing, then register a Page.ClientScript.RegisterStartupScript with the popup code, which will put some Javascript into the tag to fire off after the page loads. This should launch your new window after the processing completes.

EDIT: Reading your comment, I believe you can still use this approach, have your results stored in a session variable, and then have the popup page pull the results from there.

Dillie-O
+2  A: 

Here is the code:

protected void Button1_Click(object sender, EventArgs e)

{

// Do some server side work

string script = "window.open('http://www.yahoo.com','Yahoo')";

if (!ClientScript.IsClientScriptBlockRegistered("NewWindow"))

{

ClientScript.RegisterClientScriptBlock(this.GetType(),"NewWindow",script, true);

}

}
azamsharp
+3  A: 

Use LinkButton.PostBackUrl to set a different page to POST to, and some client script to get a new window (and the old target restored so that future postbacks work normally). The 2nd page can use PreviousPage to get access to any needed state from the original page.

<script runat="server">
   void lnk_Click(object sender, EventArgs e) {
      // Do work
   }
</script>

<script type="text/javascript">
   var oldTarget, oldAction;
   function newWindowClick(target) {
      var form = document.forms[0];
      oldTarget = form.target;
      oldAction = form.action;
      form.target = target;

      window.setTimeout(
         "document.forms[0].target=oldTarget;"
         + "document.forms[0].action=oldAction;", 
         200
      );
   }
</script>

<asp:LinkButton runat="server" PostBackUrl="Details.aspx" Text="Click Me"
  OnClick="lnk_Click"
  OnClientClick="newWindowClick('details');" />
Mark Brackett