今天,在.NET中使用了UpdatePanel,里面的输入框使用了jQuery的日历选择器:
<script type="text/javascript"> $(function(){ $("#ctl00_ContentPlaceHolder1_txtDateFrom").datepicker({ inline: true, dayNamesMin:["日","一","二","三","四","五","六"],//区域化周名为中文 firstDay:1,//每周从周一开始 //区域化月名为中文习惯 monthNames:["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"], showMonthAfterYear:true,//月份显示在年后面 yearSuffix:"年",//年份后缀字符 changeYear:true, changeMonth:true, showButtonPanel:true,//显示按钮面板 currentText:"今天",//当前日期按钮上的文本 closeText:"关闭",//关闭按钮上的文本 dateFormat:"yy-mm-dd" }); } </script>
可是,运行之后,点击"查询",页面局部刷新,发现日历选择器不出来了,找了很多资料,他们讲:
<script type="text/javascript"> function showdatepicker(){ $("#ctl00_ContentPlaceHolder1_txtDateFrom").datepicker({ inline: true, dayNamesMin:["日","一","二","三","四","五","六"],//区域化周名为中文 firstDay:1,//每周从周一开始 //区域化月名为中文习惯 monthNames:["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"], showMonthAfterYear:true,//月份显示在年后面 yearSuffix:"年",//年份后缀字符 changeYear:true, changeMonth:true, showButtonPanel:true,//显示按钮面板 currentText:"今天",//当前日期按钮上的文本 closeText:"关闭",//关闭按钮上的文本 dateFormat:"yy-mm-dd" }); } </script>
<script type="text/javascript"> $(function(){ showdatepicker(); }); </script>
然后通过捕获ScriptManager的EndRequest事件,在每次 UpdatePanel局部刷新之后执行一次jQuery初始化代码,如下所示:
<script type="text/javascript"> function load(){ Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler); } function EndRequestHandler() { showdatepicker(); } </script>
<body onload="load()">