Tôi thừa nhận đó là một chút dài, nhưng các công trình sau đây cho mọi trường hợp tôi có thể nghĩ của:
$(document).ready(function() {
'use strict';
var getParsedPostback = function getParsedPostback(self) {
/*
* self is a jQuery object. The purpose of this function is to extract the
* __doPostBack function or the WebForm_DoPostBackWithOptions function as a
* string, parse out the component arguments, and return it as a different
* function to be used as a callback. If the postback function does not exist
* as a string (in the case of a submit button, for instance) then the
* returned callback should unbind any click handlers and then trigger the
* element's click event.
*/
var postback = self.data('postback'),
trimLeft = /^\s+/,
trimRight = /\s+$/,
startingQuote = /^['"]/,
endingQuote = /['"]$/,
eventTarget,
eventArgument,
validation,
validationGroup,
actionUrl,
trackFocus,
clientSubmit;
if (postback.substring(postback.length - 1, postback.length) === ';') {
//remove the trailing ";"
postback = postback.substring(0, postback.length - 1);
}
if (postback.indexOf('javascript:') === 0) {
//remove the "javascript:"
postback = postback.substring(('javascript:').length, postback.length - 1);
}
//in case postback is in the form __doPostBack('XXXXXX','XXXXXX')
postback = decodeURIComponent(postback);
//parse by case
if (postback.indexOf('__doPostBack(') === 0) {
//postback should now be __doPostBack('XXXXXX','XXXXXX')
postback = postback.substring(('__doPostBack(').length, postback.length - 2);
postback = postback.split(',');
eventTarget = encodeURIComponent(postback[0].replace(startingQuote, '').replace(endingQuote, ''));
eventArgument = encodeURIComponent(postback[1].replace(startingQuote, '').replace(endingQuote, ''));
postback = function() {
__doPostBack(eventTarget, eventArgument);
};
} else if (postback.indexOf('WebForm_DoPostBackWithOptions(') === 0) {
//postback should now be WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('XXXXXX', 'XXXXXX', 'XXXXXX', 'XXXXXX', 'XXXXXX'))
postback = postback.substring(('WebForm_DoPostBackWithOptions(').length, postback.length - 2);
postback = postback.split(',');
eventTarget = encodeURIComponent(postback[0].replace(startingQuote, '').replace(endingQuote, ''));
eventArgument = encodeURIComponent(postback[1].replace(startingQuote, '').replace(endingQuote, ''));
validation = !!postback[2].replace(startingQuote, '').replace(endingQuote, ''); //use !! to convert string to boolean
validationGroup = encodeURIComponent(postback[3].replace(startingQuote, '').replace(endingQuote, ''));
actionUrl = encodeURIComponent(postback[4].replace(startingQuote, '').replace(endingQuote, ''));
trackFocus = !!postback[5].replace(startingQuote, '').replace(endingQuote, ''); //use !! to convert string to boolean
clientSubmit = !!postback[6].replace(startingQuote, '').replace(endingQuote, ''); //use !! to convert string to boolean
postback = function() {
__doPostBack(new WebForm_PostBackOptions(eventTarget, eventArgument, validation, validationGroup, actionUrl, trackFocus, clientSubmit));
};
} else if (postback === 'submit') {
//no apparent postback handler, must be a submit or an image
postback = function() {
//unbind the assigned click handler
self.unbind('click');
//trigger the click event
self.click();
};
}
return postback;
};
var clickHandler = function clickHandler(e) {
var postback = getParsedPostback($(this)); //get the postback as a callback
$('div#dialog').dialog('option', {
"buttons": {
"Delete all items": function() {
$(this).dialog('close');
postback(); //call the postback
},
"Cancel": function() {
$(this).dialog('close');
}
}
}).dialog('open');
e.preventDefault();
return false;
};
var storePostbacks = function storePostbacks() {
/*
* The purpose of this function is to remove any existing __doPostBack functions
* or WebForm_DoPostBackWithOptions functions and store them in the "data" for
* the element. The "getParsedPostback" function above wil make extensive use of
* the element's "data" to parse a usable callback for postback.
*/
$('input[type="submit"], input[type="button"], input[type="image"], a[href*="__doPostBack"]').each(function (i, elem) {
var self = $(elem),
postback = '';
if (typeof self.attr('onclick') !== 'undefined') {
//store the postback in data and remove from the element.
postback = self.attr('onclick');
self.removeAttr('onclick').data('postback', postback);
} else if (typeof self.attr('href') !== 'undefined') {
//store the postback in data and remove from the element.
postback = self.attr('href');
self.attr('href', '#').data('postback', postback);
} else if (self.attr('type') === 'submit' || self.attr('type') === 'image') {
//flag as a submit.
self.data('postback', 'submit');
}
});
};
storePostbacks();
$('input#<%#aspButton1.ClientID %>').click(clickHandler);
$('input#<%#aspButton2.ClientID %>').click(clickHandler);
$('input#<%#aspImageButton.ClientID %>').click(clickHandler);
$('a#<%#aspLinkButton.ClientID %>').click(clickHandler);
$('div#dialog').dialog({
"autoOpen": false
});
});
Đã kiểm tra đánh dấu sau bằng cách sử dụng ASP.Net 4.0 Framework với jQuery 1.8.2 và jQueryUI 1.9.0:
<body>
<form id="form1" runat="server">
<div>
<div id="dialog">
<p>Test of dialog.</p>
</div>
<div id="controls">
<asp:Button ID="aspButton1" runat="server" Text="aspButton1" />
<asp:LinkButton ID="aspLinkButton" runat="server">LinkButton</asp:LinkButton>
<asp:ImageButton ID="aspImageButton" runat="server" />
<asp:Button ID="aspButton2" runat="server" Text="aspButton2" />
</div>
</div>
</form>
</body>
này sẽ chạy ở bất kỳ postback, tuy nhiên, không nhất thiết chỉ trên trình mẫu do nút gửi. Bất cứ điều gì gây ra postback đầu tiên sẽ được dừng lại và xác nhận. – dwerner