{"id":269,"date":"2011-08-23T08:43:04","date_gmt":"2011-08-23T06:43:04","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=269"},"modified":"2011-08-23T08:43:04","modified_gmt":"2011-08-23T06:43:04","slug":"asp-net-postback-to-new-window-or-popup","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=269","title":{"rendered":"ASP.NET Postback to new Window or Popup"},"content":{"rendered":"<p>Previously I thought this was not possible, since the ASP.NET postback involves POST-ing the all-encompassing ASP.NET form, but I am here to tell you, that it is indeed possible to do a postback to a new window or even a popup (created with window.open). In fact, it is surprisingly simple. All you really have to know is that the &#8220;target&#8221; attribute you know from anchors (links) works exactly the same way for forms!<br \/>\n<!--more--><\/p>\n<p>Once I realized that, hijacking the postback mechanism was easy as the proverbial pie. You start out with a small piece of JavaScript that might look something like this:<\/p>\n<pre class=\"brush: jscript; gutter: true; title: ; toolbar: false; wrap-lines: false; notranslate\" title=\"\">\r\nfunction CustomPostBack(controlID, argument, target, popup) {\r\n    var oldTarget;\r\n    if (target != null || popup != null) {\r\n        oldTarget = theForm.target;\r\n        theForm.target = target;\r\n    }\r\n    if (popup != null) {\r\n        window.open('', popup.name, 'width=' + popup.width + ',height=' + popup.height);\r\n        theForm.target = popup.name;\r\n    }\r\n\r\n    __doPostBack(controlID, argument);\r\n\r\n    if (target != null || popup != null) {\r\n        theForm.target = oldTarget;\r\n    }\r\n}\r\n<\/pre>\n<p>Let me start by explaining the function parameters:<\/p>\n<ul>\n<li><strong>controlID<\/strong>: The unique ID (Control.UniqueID) of the control that will be receiving the postback event. This control has to implement the IPostBackEventHandler interface which defines the void RaisePostBackEvent(string eventArgument) method.<\/li>\n<li><strong>argument<\/strong>: The string argument that is passed to the control&#8217;s postback event handler.<\/li>\n<li><strong>target<\/strong>: The value for the HTML &#8220;target&#8221; attribute. Use &#8220;_blank&#8221; to post back to a new window or null if you want to post to a popup window.<\/li>\n<li><strong>popup<\/strong>: JSON structure of the form { name: <em>name of the popup<\/em>, width: <em>width of the popup<\/em>, height: <em>height of the popup<\/em> }. If &#8220;popup&#8221; is not null, the target argument is ignored.<\/li>\n<\/ul>\n<p>The rest is fairly straight forward. We start by storing the form&#8217;s original target so that we can restore it in the end. If we don&#8217;t use a popup as target, we set &#8220;theForm&#8221;&#8216;s target to whatever value is passed in to the function (btw: &#8220;theForm&#8221; is defined by the regular ASP.NET initialization script). If we want to use a popup as postback target, we first open a new window with the given name and then set the form&#8217;s target to that window&#8217;s name. This causes the POST to be sent to the popup window. Once the target is properly set, we do a regular ASP.NET post back and restore the original form target &#8211; if we don&#8217;t do that, all the &#8220;normal&#8221; postbacks would also be sent to the new window or popup and we don&#8217;t want that.<\/p>\n<p>There you go, it&#8217;s as simple as that. This works in all browsers that I have tested (FF, Chrome, IE9 and it even works in IE9&#8217;s &#8220;broken mode&#8221;) and is as far as I can tell standard HTML compliant.<\/p>\n<p>Hope this saves somebody some time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously I thought this was not possible, since the ASP.NET postback involves POST-ing the all-encompassing ASP.NET form, but I am here to tell you, that it is indeed possible to do a postback to a new window or even a popup (created with window.open). In fact, it is surprisingly simple. All you really have to &hellip; <\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5,13],"tags":[],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-executor","4":"has-more-link","5":"post-269","7":"format-standard","8":"category-asp","9":"category-html-css"},"acf":[],"views":41730,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/269"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=269"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/269\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=269"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}