Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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):
ASP.NET
<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:
VB
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();}")
Posted
Updated 24-Jul-13 2:54am
v3
Comments
[no name] 24-Jul-13 6:16am    
check you may enable autopostback in your textbox
VishwaKL 24-Jul-13 8:12am    
check the events you may registered manually or else check the auto post back property of the control. if you made it true, make is false or remove that
woopsydoozy 24-Jul-13 8:56am    
Thanks to you both--I updated to show some code. No autopostback, and no registered events that I don't want to happen. I just don't expect the onchange to happen immediately when the grid is bound.

1 solution

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:
ASP.NET
<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:
JavaScript
//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:
VB
Dim txtControl As TextBox = e.Row.Cells(GridCols.SubtaskCompleteDt).Controls(1)
txtControl.Attributes.Add("onblur", "if (!validateDateString(this)) {this.value = ''; this.focus();}")
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900