Quantcast
Channel: Rick Schott :: devlpr.net » jQuery
Viewing all articles
Browse latest Browse all 10

Supporting WebForm Postbacks in a Non-WebForms application

$
0
0

This is a little crazy but it does work. I have a mobile application framework to uses a couple legacy ASP.NET controls. I had a need for them to support postbacks. The real problem I have is that I don’t support ViewState, I don’t have a ScriptManager and my pages load via AJAX inside the jQuery Mobile framework.

So, even when ASP.NET recognized these controls needs more, my mobile framework ignores its, so what do to? I added some ASP.NET shims for __doPostBack and WebForm_FireDefaultButton.

<!doctype html>
<html>
<head>
     ...
</head>
<body>
    <form id="Form1" runat="server" action="">
         ...
    </form>
</body>
</html>
var theForm; //stupid .net shim

$(function () {
    try {
        theForm = document.forms['Form1'];
        if (!theForm) {
            theForm = document.Form1;
        }
    } catch (e) {
    } 
});

var __defaultFired = false;
function WebForm_FireDefaultButton(event, target) {
    var __nonMSDOMBrowser = (window.navigator.appName.toLowerCase().indexOf('explorer') == -1);
    if (!__defaultFired && event.keyCode == 13 && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) {
        var defaultButton;
        if (__nonMSDOMBrowser) {
            defaultButton = document.getElementById(target);
        } else {
            defaultButton = document.all[target];
        }
        if (defaultButton && typeof(defaultButton.click) != "undefined") {
            __defaultFired = true;
            defaultButton.click();
            event.cancelBubble = true;
        if (event.stopPropagation) event.stopPropagation();
            return false;
        }
    }
    return true;
}

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        if (!theForm.__EVENTTARGET) {
            var _eventtarget = '<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />';
            $('.aspNetHidden').append(_eventtarget);
        }
        if (!theForm.__EVENTARGUMENT) {
             var _eventarg = '<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />';

            $('.aspNetHidden').append(_eventarg);
        }
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

Viewing all articles
Browse latest Browse all 10

Trending Articles