asp.net UpdatePanel 中的 jquery ui datepicker
我有一个网页,我在 asp.net 文本框上使用 jQuery UI 日期选择器,该文本框位于 UpdatePanel 内。这是我大致所做的描述
<script type="text/javascript">
$(document).ready( function() { $(".datepicker").datepicker(); } );
</script>
<asp:UpdatePanel ... >
...
<asp:TextBox runat="server" CssClass="datepicker" />
<asp:Button runat="server" />
...
</asp:UpdatePanel>
当我第一次加载页面时,一切正常。单击文本框内部时,会弹出日期选择器。但是,当我单击按钮并执行异步回发时,当我再次单击该字段时,日期选择器不再弹出。
我知道问题是因为 UpdatePanel 在更新时完全替换了所有包含的 HTML,因此实际上,它是一个新的文本字段,尚未使用日期选择器功能进行初始化。
我想我不应该在这里使用 $(document).ready() 来初始化我的日期选择器,但是哪里是放置初始化代码的好地方?或者有没有办法可以在 AJAX 更新后重新触发初始化代码?
I have a web page, where I'm using a jQuery UI datepicker on an asp.net textbox, which is located inside an UpdatePanel. Here is a description of what I do roughly
<script type="text/javascript">
$(document).ready( function() { $(".datepicker").datepicker(); } );
</script>
<asp:UpdatePanel ... >
...
<asp:TextBox runat="server" CssClass="datepicker" />
<asp:Button runat="server" />
...
</asp:UpdatePanel>
When I first load the page, everything works fine. When clicking inside the textbox, the datepicker pops up. But when I click the button, and an async postback is executed, the datepicker no longer pops up, when I click the field again.
I know that the problem is because the UpdatePanel completely replaces all the contained HTML when it is updated, so in effect, it is a new text field, which has not been initialized with the datepicker functionality.
I guess that I should not use $(document).ready() here to initialize my datepickers, but where is a good place to place the initialization code? Or is there a way that I can retrigger the initialization code after an AJAX update?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将类名'.datepicker'替换为页面内对象的名称。
例子:
Replaces the class name '.datepicker' by the name of the object within the page.
Example:
在后面添加脚本,这就是我所做的。
add the script behind , that's what I do.
将此脚本添加到页面末尾。
将initialize() 函数替换为您每次从updatepanel 进行部分回发时想要运行的任何代码。
Add this script at the end of your page.
Replace the initialize() function with whatever code you want to run every time there is a partial postback from an updatepanel.