为什么我的 jQuery UI 日期选择器在初始页面加载时工作正常,但不能用于回发?
我一直在尝试将 jQuery UI Datepicker 集成到我们的 ASP.NET WebForms 应用程序中。该应用程序使用母版页为所有页面提供共同的外观,并且所有内容页面都构建在 UpdatePanel 的 ContentTemplate 内。我创建了一个用户控件来包装日期选择器的功能,并允许在运行时为代码隐藏中的每个日期设置 minDate、maxDate、验证器等。我还需要处理 ASP.NET 生成客户端元素 ID 的方式。
在过去的一周里,我进行了广泛的搜索,发现了很多让它发挥作用的技巧。这是我现在所处位置的一个稍微简化的版本。
<asp:TextBox ID="txtTB" runat="server"
MaxLength="10"
CssClass="dateBox" />
<script type="text/javascript">
// This function enables the datepicker behavior for this textbox by its ClientID.
function <%=txtTB.ClientID %>() {
$("#<%=txtTB.ClientID %>").datepicker({
changeMonth: true, changeYear: true,
showOn: 'button',
buttonImage: "<%=Request.ApplicationPath %>/images/calendarIcon.gif",
buttonImageOnly: true, buttonText: '', duration: '',
onSelect: function() { }
});
};
// Register the above function to execute on initial page load...
$(document).ready(function(){ <%=txtTB.ClientID %>(); }) ;
// ...and also after any update panel it's on has been refreshed.
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(<%=txtTB.ClientID %>);
</script>
我省略了验证器和其他一些我认为与该问题无关的标记,但这是用户控件的核心——文本框和启用日期选择器功能的 jQuery 脚本。基本思想是利用 ASP.NET 的客户端 ID 来:
- 为该特定控件的 JavaScript 函数提供唯一的函数名称
- 使用 jQuery 注册该函数以在初始页面后启用日期选择器 load
- 向 ASP.NET 的 PageRequestManager 注册相同的函数,以在回发后保持启用日期选择器
项目 1 和 2 工作得很好。但是,回发后,日期选择器功能丢失,只剩下一个文本框。
但是等等!这还不是全部。给定页面由多个面板组成,一次只能看到其中一个面板。仅通过使当前面板的 Visible 属性为 true 来选择它们。所有其他面板的 Visible 属性均设置为 false。
现在得到这个:第一个面板上的日期选择器(在初始页面加载时显示的日期选择器)按预期工作。第二个面板上的那些就不会出现。返回第一页,日期选择器再次出现。
有人知道我哪里出错了吗?
I've been trying to integrate the jQuery UI Datepicker into our ASP.NET WebForms application. The application uses master pages to provide a common look to all pages, and all of the content pages are built inside the ContentTemplate of an UpdatePanel. I've created a user control to wrap the datepicker's functionality and allow setting of the minDate, maxDate, validators, etc. for each date from the codebehind at runtime. I also need to deal with the way ASP.NET generates client element IDs.
Over the last week, I've searched high and low, and found lots of tips for getting it to work. Here is a somewhat simplified version of where I'm at right now.
<asp:TextBox ID="txtTB" runat="server"
MaxLength="10"
CssClass="dateBox" />
<script type="text/javascript">
// This function enables the datepicker behavior for this textbox by its ClientID.
function <%=txtTB.ClientID %>() {
$("#<%=txtTB.ClientID %>").datepicker({
changeMonth: true, changeYear: true,
showOn: 'button',
buttonImage: "<%=Request.ApplicationPath %>/images/calendarIcon.gif",
buttonImageOnly: true, buttonText: '', duration: '',
onSelect: function() { }
});
};
// Register the above function to execute on initial page load...
$(document).ready(function(){ <%=txtTB.ClientID %>(); }) ;
// ...and also after any update panel it's on has been refreshed.
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(<%=txtTB.ClientID %>);
</script>
I've left out the validators and some other markup that I don't think is related to the issue, but that's the meat of the user control--a textbox, and the jQuery script that enables the datepicker functionality. The basic idea is to leverage ASP.NET's client IDs to:
- Provide a unique function name for the JavaScript function for this specific control
- Register that function with jQuery to enable the datepicker after the initial page
load - Register that same function with ASP.NET's PageRequestManager to keep the datepickers enabled after postbacks
Items 1 and 2 are working great. However, after a postback, the datepicker functionality is lost and I'm left with just a textbox.
But wait! That's not all. A given page consists of multiple panels, only one of which is visible at a time. These are selected by only having the Visible property true for the current panel. The Visible property for all of other panels are set to false.
Now get this: The datepickers on the first panel, the one displayed upon initial page load, work as expected. Those on the second panel just don't show up. Going back to the first page, the datepickers show up again.
Does anyone have any ideas where I'm going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
虽然 $(document).ready() 非常适合一次性初始化例程,但如果您的代码需要在每次部分回发后重新运行,它会让您陷入困境。
jQuery v1.3+ 中添加的 LiveQuery 功能对此有所帮助,但仅适用于有限的功能集。
例如,如果我们想将 jQueryUI 日期选择器添加到前面示例中的 TextBox,该怎么办?将其添加到 $(document).ready() 中会很好用,直到发生部分回发。然后,UpdatePanel 的新 TextBox 元素将不再连接日期选择器。
上述答案取自以下链接。我已经在我的一个项目中测试并实现了这一点。
http://encosia.com/ 2009/03/25/document-ready-and-pageload-are-not-the-same/
While $(document).ready() is ideal for one-time initialization routines, it leaves you hanging if you have code that needs to be re-run after every partial postback.
The LiveQuery functionality added in jQuery v1.3+ helps with this, but only works for a limited set of functionality.
For example, what if we wanted to add a jQueryUI datepicker to the TextBox in the previous example? Adding it in $(document).ready() would work great, until a partial postback occurred. Then, the UpdatePanel’s new TextBox element would no longer have the datepicker wired up to it.
This above Answer is taken from the below link. I have tested and implemented this in one of my project.
http://encosia.com/2009/03/25/document-ready-and-pageload-are-not-the-same/
我认为更简洁的方法是使用 ClientScriptManager.RegisterStartupScript。
Javascript:
在你的代码后面(
OnPreRender
也许):I think a cleaner way to do this is to use
ClientScriptManager.RegisterStartupScript
.Javascript:
In your code behind (
OnPreRender
perhaps):抱歉回复晚了。
为什么不尝试这个:
然后使用以下触发器将文本框放入更新面板中:
Sorry for the late reply.
Why not try this:
then put the textbox in an updatepanel with the following trigger:
嗯,
这是这个问题的解决方案。下面的链接解释了为什么日期选择器在ajax回发后不起作用以及使其在ajax中工作的解决方案。
http://jquerybyexample.blogspot.com/2010 /08/jquery-datepicker-does-not-work-after.html
Well,
Here is the solution of this problem. Below link explains why the datepicker doesn't work after ajax postback and the solution to make it work within ajax.
http://jquerybyexample.blogspot.com/2010/08/jquery-datepicker-does-not-work-after.html
当您将控件的
Visible
属性设置为false
时,它根本不会呈现。这意味着当页面首次加载时,不可见面板内的 JavaScript不存在。
要解决此问题,您可以使用 CSS (
display: none
) 而不是Visible
属性来隐藏面板。When you set a control's
Visible
property tofalse
, it doesn't get rendered at all.That means that the Javascript inside the invisible panels did not exist when the page was first loaded.
To solve this, you can hide your panels using CSS (
display: none
) instead of theVisible
property.在更新面板中尝试此操作
Try this inside the update panel
只需在 Page_Load 代码后面使用下面的代码..
我实现了这个并且工作起来就像一个魅力
Just Simply use bellow code in Page_Load code behind..
I implemented this and works like a charm
如果您使用更新面板,请不要惊慌 单击此处 放松一下。
详细解答
if you are using update panel then don't get panic just Click here and relax.
Elaborated answer
该解决方案对我有帮助。日期选择器值必须位于 pageLoad() 函数中,以便日期选择器在回发时显示。
这是我的工作代码
The solution helped me. The date picher values must be in pageLoad() function inorder for the date picker to show on postbacks.
Here is my working code