在我的 asp.net 页面上,我需要控制脏控件。我在 javascript 方面不是那么好,所以我在互联网上找到了这样做的解决方案:
<script type="text/javascript" language="javascript">
//Demo of completely client side dirty flag.
function setDirty() {
document.body.onbeforeunload = showMessage;
//debugger;
document.getElementById("DirtyLabel").className = "show";
}
function clearDirty() {
document.body.onbeforeunload = "";
document.getElementById("DirtyLabel").className = "hide";
}
function showMessage() {
return ""
}
function setControlChange() {
if (typeof (event.srcElement) != 'undefined') {
event.srcElement.onchange = setDirty;
}
}
document.body.onclick = setControlChange;
document.body.onkeyup = setControlChange;
window.onunload = function (sender, eventArgs) {
if (window.opener != null) {
window.opener.ClearBlocker();
if (window.opener.TempClientReturnFunction != null)
window.opener.TempClientReturnFunction = window.opener.ReturnFunction;
}
}
</script>
但如果我有 7 页需要控制脏控件,那将是过多的冗余代码。有什么方法可以创建一些我可以调用函数的类/库,或者有没有更聪明的方法呢?