CalendarExtender意外触发onchange [英] CalendarExtender firing onchange unexpectedly

查看:127
本文介绍了CalendarExtender意外触发onchange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码正常运行,但有人可以解释为什么这是一个问题吗?我有一个CalendarExtender附加到一个GridView的一个文本框中,该文本框位于UpdatePanel内部(不知道是否有任何问题超出了CalendarExtender)。当页面上的下拉列表发生更改时,我构建了一个数据表并绑定了GridView。在RowDataBound事件中,我向每行中的控件添加了一些处理程序 - 重要的是处理链接文本框的onchange事件。功能大部分都很好,但我发现在绑定网格后我得到了一系列回发,虽然没有造成任何实际问题,但处理得不好。事实证明,这是一个正在开火的onchange事件,一次是对于新加载的网格的每一行,并且它作为新请求而不是回发而被触发。由于某种原因,我不知道解决方案是添加CalendarExtender控件的Format属性。有关为何发生这种情况的任何指导?



相关标记(带修复):

 <   ItemTemplate  >  
< asp:Textbox 宽度 = 92% ID = txtSubCompleteDt runat = < span class =code-keyword> server 文本 =' <%# Eval( Subtask_Complete_Dt)%>' > < / asp:Textbox >
< asp:CalendarExtender ID = dtpSubtaskComplete runat = server TargetControlID = txtSubCompleteDt 格式 = MM / dd / yyyy > < / asp:CalendarExtender >
< / ItemTemplate >





背后的相关代码:

  Dim  eventHandler 作为 字符串 = 字符串 .Format(  ajaxPostbackTrigger('{0}:{1}:{{0}}' );,EVENT_KEY,e.Row.RowIndex)
Dim txtControl As TextBox = e.Row.Cells(GridCols.SubtaskCompleteDt).Controls( 1
strFnCall = String .Format(eventHandler,Convert.ToInt32(GridCols.SubtaskCompleteDt))
txtControl.Attributes.Add( onchange if(!validateDateString(this)){return false ;} else {onkeyupEnable();& strFnCall& }
txtControl.Attributes.Add( onblur if (!validateDateString(this)){this.value =''; this.focus();}

解决方案

< blockquote>事实证明,将Format属性添加到CalendarExtender只解决了一组加载事件的问题。我有一个单独的textbox-CalendarExtender对,当它改变时,我自动将值传播到网格中的每个textbox-calendarExtender对;当DataBind发生时,同样的错误重新出现。我无法在一个简单的项目中重现,但我发现我不是唯一一个遇到这个问题的人 - 请参阅: http://forums.asp.net/t/1679414.aspx [ ^ ]



一些观察结果:

1)删除CalendarExtender可以防止错误,但会删除功能

2)在onchange上添加一个警报,然后由于某种原因回发到服务器,清除了与主线程断开连接的不需要的回发问题,但它们仍然会发生

3)上面链接中的海报声称不同格式(yyyy / MM / dd)也有帮助 - 我没试过这个



我是什么最后做的是按照上面的链接,重新设计我的功能,使其不存在于Textbox的onchange事件中,而是存在于CalendarExtender的OnClientDateSelectionChanged中。之后很多讨厌的小重构,它运行没有错误。



加价:

 <   itemtemplate  >  
< asp:textbox width = 92% id = txtSubCompleteDt runat = server xmlns:asp = < span class =code-keyword>#unknown > < / asp:textbox >
< asp:calendarextender id = dtpSubtaskComplete runat = server targetcontrolid = txtSubCompleteDt format = MM / dd / yyyy onclientdateselectionchanged = dateSelectionChanged xmlns:asp = #unknown > < ; / asp:calendarextender >
< / itemtemplate >



和脚本:

  //  网格的calendarExtender的日期更改事件处理程序 
function dateSelectionChanged(sender){
var selectedDate = sender.get_selectedDate()。localeFormat(sender.get_format());
if (!validateDateString2(selectedDate)){
return ;
}
else {
onkeyupEnable();
ajaxPostbackTrigger(' gridchg:' +(sender._element.parentElement.parentElement.rowIndex - 1 )+ ' :3:' + selectedDate);
}
}





代码落后:

  Dim  txtControl  As  TextBox = e.Row.Cells(GridCols.SubtaskCompleteDt)。控件( 1 
txtControl.Attributes.Add( onblur if(!validateDateString(this)){this.value ='' ; this.focus();}


I got my code working, but can someone please explain why this is a problem? I had a CalendarExtender attached to a textbox in a column of a GridView that is inside an UpdatePanel (don't know if any of that matters beyond the CalendarExtender). When a dropdownlist on the page is changed, I build a data table and bind the GridView. On the RowDataBound event, I add some handlers to the controls in each row--the one that mattered was the onchange event for the linked textbox. The functionality was mostly fine, but I found that I was getting a series of postbacks after binding the grid that, while not causing any real problems, were not handled well. Turns out it was the onchange event that was firing, once for each row of the freshly-loaded grid, and it was firing as a new request, not a postback. The solution, for some reason I don't know, was to add the Format attribute of the CalendarExtender control. Any guidance on why this happened?

Relevant markup (with the fix):

<ItemTemplate>
    <asp:Textbox Width="92%" ID="txtSubCompleteDt" runat="server" Text='<%# Eval("Subtask_Complete_Dt") %>'></asp:Textbox>
    <asp:CalendarExtender ID="dtpSubtaskComplete" runat="server" TargetControlID="txtSubCompleteDt" Format="MM/dd/yyyy" ></asp:CalendarExtender>
</ItemTemplate>



Relevant code behind:

Dim eventHandler As String = String.Format("ajaxPostbackTrigger('{0}:{1}:{{0}}');", EVENT_KEY, e.Row.RowIndex)
Dim txtControl As TextBox = e.Row.Cells(GridCols.SubtaskCompleteDt).Controls(1)
strFnCall = String.Format(eventHandler, Convert.ToInt32(GridCols.SubtaskCompleteDt))
txtControl.Attributes.Add("onchange", "if (!validateDateString(this)) {return false;} else {onkeyupEnable();" & strFnCall & "}")
txtControl.Attributes.Add("onblur", "if (!validateDateString(this)) {this.value = ''; this.focus();}")

解决方案

Turns out that adding the Format attribute to the CalendarExtender only solved the problem for one set of load events. I had a separate textbox-CalendarExtender pair, and when that changed, I automatically propagated the value to every textbox-calendarExtender pair in the grid; when that DataBind occurred, the same error resurfaced. I was unable to reproduce in a bare-bones project, but I found I'm not the only one to experience the problem--see this: http://forums.asp.net/t/1679414.aspx[^]

Some observations:
1) removing the CalendarExtender prevents the error, but removes functionality
2) adding an alert in the onchange before it posts back to the server for some reason clears up the issue of the unwanted postbacks being disconnected from the main thread, but they still occur
3) the poster in the link above claims a different format (yyyy/MM/dd) also helps--I did not try this

What I finally did was to follow the link above, reworking my functionality so that instead of existing in the Textbox's onchange event, it exists in the CalendarExtender's OnClientDateSelectionChanged. Many annoying little refactors later, it runs without error.

Markup:

<itemtemplate>
   <asp:textbox width="92%" id="txtSubCompleteDt" runat="server" xmlns:asp="#unknown"></asp:textbox>
   <asp:calendarextender id="dtpSubtaskComplete" runat="server" targetcontrolid="txtSubCompleteDt" format="MM/dd/yyyy" onclientdateselectionchanged="dateSelectionChanged" xmlns:asp="#unknown"></asp:calendarextender>
</itemtemplate>


and script:

//date change event handler for grid's calendarExtender
function dateSelectionChanged(sender) {
    var selectedDate = sender.get_selectedDate().localeFormat(sender.get_format());
    if (!validateDateString2(selectedDate)) {
        return false;
    }
    else {
        onkeyupEnable();
        ajaxPostbackTrigger('gridchg:' + (sender._element.parentElement.parentElement.rowIndex - 1) + ':3:' + selectedDate);
    }
}



Code behind:

Dim txtControl As TextBox = e.Row.Cells(GridCols.SubtaskCompleteDt).Controls(1)
txtControl.Attributes.Add("onblur", "if (!validateDateString(this)) {this.value = ''; this.focus();}")


这篇关于CalendarExtender意外触发onchange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆