更新!我在CodeProject.com上更新下面的代碼。請搜索「將ASPX頁面加載到其他頁面」。
我正在嘗試做類似的事情:嘗試將ASPX頁面的內容加載到HTML頁面中,並在提交內容時使用ASPX頁面背後的代碼。也許下面的代碼可以幫助你。謹防我只是JQuery的初學者!如果有人可以改進下面的代碼...請做!
Demo.htm
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script type="text/javascript" src="Javascript/LiQuickBootstrapper.js"></script>
<div id="portalViewContent" class="portalViewContent">
</div>
<script type='text/javascript'>
$(document).ready(function() {
loadPage('/Widget/PostCodeCheck.aspx', 'portalViewContent', 'contentWidget');
})
</script>
LiQuickBootstrapper.js
function submit_handler(formObj, widgetUri) {
var submitObj = $(formObj).data('objClicked');
var submitObjID = submitObj.attr('id');
var formData = formObj.serialize()
+ '&'
+ encodeURI(submitObj.attr('id'))
+ '='
+ encodeURI(submitObj.attr('value'))
;
$(formObj).fadeOut('2000');
$(formObj).promise().done(function() {
$.post(widgetUri, formData)
.done(function (xData) {
var newSubmitObj = $($.parseHTML(xData)).find("#" + submitObjID);
var newForm = newSubmitObj.parents('form');
formObj.html(newForm.html());
$(formObj).fadeIn('2000');
})
.fail(function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
});
});
return false;
}
function loadPage(widgetUri, containerIDLocal, containerIDRemote) {
var containerObjLocal = $('#' + containerIDLocal);
$(containerObjLocal).fadeOut('2000');
$(containerObjLocal).promise().done(function() {
$.post(widgetUri)
.done(function (xData) {
var externalHTML = $.parseHTML(xData);
var containerObjRemote = $(externalHTML).filter('#' + containerIDRemote);
containerObjLocal.html(containerObjRemote.html());
var forms = containerObjLocal.children('form');
$(forms).submit(function (event) {
event.preventDefault();
submit_handler($(event.currentTarget), widgetUri);
});
$(forms).click(function (event) {
$(this).data('objClicked', $(event.target));
});
$(forms).each(function() {
$(this).data('widgetUri', widgetUri);
});
__doPostBack = function (t, a) {
__myDoPostBack(t, a);
}
$(containerObjLocal).fadeIn('2000');
})
.fail(function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
});
});
}
function __myDoPostBack(eventTarget, eventArgument) {
var submitObj = $("#" + eventTarget);
var theForm = submitObj.parents('form:first');
var submitObjID = submitObj.attr('id');
var widgetUri = $(theForm[0]).data('widgetUri');
$(theForm[0]).find(':hidden#__EVENTTARGET')[0].value = eventTarget;
$(theForm[0]).find(':hidden#__EVENTARGUMENT')[0].value = eventArgument;
var formData = theForm.serialize();
$(theForm).fadeOut('2000');
$(theForm).promise().done(function() {
$.post(widgetUri, formData)
.done(function (xData) {
var newSubmitObj = $($.parseHTML(xData)).find("#" + submitObjID);
var newForm = newSubmitObj.parents('form');
theForm.html(newForm.html());
$(theForm).fadeIn('2000');
})
.fail(function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
});
});
return false;
}
發送搜索按鈕,而不是回傳 – Adil
Ajax調用'搜索button'已經綁定任何事件呢? – Jai
Hi @Jai!在ExternalPage.aspx中,搜索按鈕有一個服務器端事件,它從數據庫搜索並顯示結果。 –