您现在的位置是:网站首页> 编程资料编程资料
UpdatePanel和jQuery不兼容 局部刷新jquery失效_实用技巧_
2023-05-25
326人已围观
简介 UpdatePanel和jQuery不兼容 局部刷新jquery失效_实用技巧_
在做项目中发现,在使用了UpdatePanel的地方,局部刷新后,jquery失效了。
后来网上一查,才发现,jquery中的ready事件会在DOM完全加载后运行一次,而当我们实用了UpdatePanel,它只局部更新,并未重新加载页面所有Dom,所以jquery中ready事件将不会再次执行。所以,我们可以将ready事件中执行的代码提取出来,然后通过捕获ScriptManager的EndRequest事件,在每次 UpdatePanel局部刷新之后执行一次jQuery初始化代码:
//处理ajax和ScriptManager的冲突
function load() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}
function EndRequestHandler() {
$(function () {
$("tbody").find("input:checkbox").each(function (key, val) {
$(val).click(function () {
var cbxId = $(this).attr("id");
var state = $(this).attr("checked");
$.post("Ajax/UpdateStatus.ashx", { "id": cbxId, "isChecked": state, "fid": "SamID" }, isReturnStatus);
});
});
$("thead").find("input:checkbox").click(
function () {
if (confirm("确定要更新这一列数据吗?") == true) {
var cbxId = $(this).attr("id");
var name = cbxId.substr(16);
var v = "tbody ." + name + " input[type='checkbox']";
if ($(this).attr("checked") == "checked") {
$(v).attr("checked", true);
}
else {
$(v).attr("checked", false);
}
var state = $(this).attr("checked");
$.post("Ajax/UpdateStatus.ashx", { "id": cbxId, "isChecked": state }, isReturnStatus);
}
else {
if ($(this).attr("checked") == "checked") {
$(this).attr("checked", false);
}
else {
$(this).attr("checked", true);
}
}
});
});
initCheckedStaus();
}
后来网上一查,才发现,jquery中的ready事件会在DOM完全加载后运行一次,而当我们实用了UpdatePanel,它只局部更新,并未重新加载页面所有Dom,所以jquery中ready事件将不会再次执行。所以,我们可以将ready事件中执行的代码提取出来,然后通过捕获ScriptManager的EndRequest事件,在每次 UpdatePanel局部刷新之后执行一次jQuery初始化代码:
复制代码 代码如下:
//处理ajax和ScriptManager的冲突
function load() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}
function EndRequestHandler() {
$(function () {
$("tbody").find("input:checkbox").each(function (key, val) {
$(val).click(function () {
var cbxId = $(this).attr("id");
var state = $(this).attr("checked");
$.post("Ajax/UpdateStatus.ashx", { "id": cbxId, "isChecked": state, "fid": "SamID" }, isReturnStatus);
});
});
$("thead").find("input:checkbox").click(
function () {
if (confirm("确定要更新这一列数据吗?") == true) {
var cbxId = $(this).attr("id");
var name = cbxId.substr(16);
var v = "tbody ." + name + " input[type='checkbox']";
if ($(this).attr("checked") == "checked") {
$(v).attr("checked", true);
}
else {
$(v).attr("checked", false);
}
var state = $(this).attr("checked");
$.post("Ajax/UpdateStatus.ashx", { "id": cbxId, "isChecked": state }, isReturnStatus);
}
else {
if ($(this).attr("checked") == "checked") {
$(this).attr("checked", false);
}
else {
$(this).attr("checked", true);
}
}
});
});
initCheckedStaus();
}
相关内容
- IE下document.referrer 拒绝访问的解决方法_实用技巧_
- GridView自定义删除操作的具体方法_实用技巧_
- asp.net Textbox服务器控件_实用技巧_
- SQLServer 在Visual Studio的2种连接方法_实用技巧_
- GridView使用CommandField删除列实现删除时提示确认框_实用技巧_
- DataGrid中实现超链接的3种方法_实用技巧_
- .Net下二进制形式的文件(图片)的存储与读取详细解析_实用技巧_
- DataSet.Tables[].Rows[][]的用法详细解析_实用技巧_
- 如何在asp.net中使用FreeTextBox控件_实用技巧_
- 将FreeTextBox做成控件添加到工具箱中的具体操作方法_实用技巧_
