首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

关于updatepanel回传之后JS失效有关问题!

2012-03-18 
关于updatepanel回传之后JS失效问题!!!!!!!!!!!!!!!!!!!我找了写资料,貌似是应为UpdatePanel来回往返不会

关于updatepanel回传之后JS失效问题!!!!!!!!!!!!!!!!!!!
我找了写资料,貌似是应为UpdatePanel来回往返不会保留 Javascript 或其他客户端技术(如 Adobe Flash)生成的任何效果。
我也找到了解决办法:
http://msdn.microsoft.com/zh-cn/library/bb311028.aspx
http://hi.baidu.com/a_longge2009/blog/item/9956cb23ca971ea94723e849.html
其实就是用Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
但是我试了,貌似不行啊....是不是我的JS有问题???我对JS不大懂,请大家帮帮忙!!

HTML code
function lr(overHide, scrollUl, leftBtn, rightBtn, num) {                    var doc = document,                    overHide = doc.getElementById(overHide),                    scrollUl = doc.getElementById(scrollUl),                    liArr = scrollUl.getElementsByTagName('li'),                    len = liArr.length,                    leftBtn = doc.getElementById(leftBtn),                    rightBtn = doc.getElementById(rightBtn),                    overWidth = overHide.offsetWidth;                    scrollUl.style.width = len * num + 'px';                    var inWidth = scrollUl.offsetWidth;                    function checkBtn(overHide, scrollUl, leftBtn, rightBtn) {//判断左右按钮显示与隐藏                        if (overWidth >= inWidth) {//内容不够不需要左右按钮                            leftBtn.style.visibility = 'hidden';                            rightBtn.style.visibility = 'hidden';                        } else if (overHide.scrollLeft == 0) {//右显示左隐藏                            leftBtn.style.visibility = 'hidden';                            rightBtn.style.visibility = 'visible';                        } else if ((overHide.scrollLeft + overWidth) == inWidth) {//左显示右隐藏                            leftBtn.style.visibility = 'visible';                            rightBtn.style.visibility = 'hidden';                        } else if ((overHide.scrollLeft + overWidth) < inWidth && overHide.scrollLeft > 0) {//左右均显示                            leftBtn.style.visibility = 'visible';                            rightBtn.style.visibility = 'visible';                        }                    }                    checkBtn(overHide, scrollUl, leftBtn, rightBtn);                    function stepL(obj, step) {                        var n = 0;                        function scrollL(obj, step) {                            if (n < step) {                                obj.scrollLeft += 5;                                n += 5;                            } else {                                clearTimeout(comeon)                            }                            checkBtn(overHide, scrollUl, leftBtn, rightBtn)                            var comeon = setTimeout(function () { scrollL(obj, step) }, 20);                        }                        scrollL(obj, step);                    }                    function stepR(obj, step) {                        var n = step;                        function scrollR(obj, step) {                            if (n > 0) {                                obj.scrollLeft -= 5;                                n -= 5;                            } else {                                clearTimeout(comeon)                            }                            checkBtn(overHide, scrollUl, leftBtn, rightBtn)                            var comeon = setTimeout(function () { scrollR(obj, step) }, 20);                        }                        scrollR(obj, step);                    }                    leftBtn.onclick = function () {                        stepR(overHide, num)                    }                    rightBtn.onclick = function () {                        stepL(overHide, num)                    }                }                lr('scroll_cont', 'scroll_ul', 's_L', 's_R', 122);


[解决办法]
JS失效可能是由于Ajax异步更新Html导致某些元素找不到
------解决方案--------------------


<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args)
{
var elem = args.get_postBackElement();
ActivateAlertDiv('visible', 'AlertDiv', elem.value + ' processing...');
}
function EndRequestHandler(sender, args)
{
ActivateAlertDiv('hidden', 'AlertDiv', '');
}
function ActivateAlertDiv(visstring, elem, msg)
{
var adiv = $get(elem);
adiv.style.visibility = visstring;
adiv.innerHTML = msg;
}
</script>
MSDN的方法试了没?
完全可以用jquery ajax
[解决办法]
你需要
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "MoveLayer();", true);
这种格式进行注册客户端的函数

热点排行