views:

200

answers:

2

I have an asp:TextBox with asp:RegularExpressionValidator to validate if it's a number. Obviously an onchange event will be attached to this textbox while rendering. Also I add a change event at $(document).ready to make some calculation when the value is changed.

<asp:TextBox id="myText" runat="server" />
<asp:regularexpressionvalidator id="myRev" ControlToValidate="myText" runat="server">*</asp:regularexpressionvalidator>

$(document).ready(function(){
    $('[id$=myText]').bind('change',function(){
           //do something
    }).change();      //force the change event at the very beginning
});

My function will be executed later than the .net generated js because of the register time. But the .net js throws an error. I traced in the js:

   function ValidatorOnChange(event) {
       ...
   }

and found that all of event.fromElement,event.toElement,event.srcElement are null which causes the exception. Did I do something wrong? Any solutions? Thanks.

EDIT

It's proved to be a MS bug, working fine in .NET 4.

A: 

Pack your calculations in a function and call it on ready event instead of triggering a change:

$(document).ready(function(){
    $('[id$=myText]').bind('change',function(){
           doCalc(); // or doCalc(this) or whatever you need
    });
    doCalc();
});
andr
Without knowing the inside of that method that's a long shot, and it can be done *much* shorter: `$('[id$=myText]').change(doCalc);`
Nick Craver
Yeah, valid point, usually I do it shorter style, focused on the main point now to make it more visible.
andr
@andr I wonder why the event is fired incorrectly. I'm using the correct grammar.
Danny Chen
+1  A: 

Including Pointy's point (I crack myself up) about ID, you can re-write it like this:

$(function(){
  $('#<%=myText.ClientID%>').change(function() {
    //stuff
  }).triggerHandler('change');
});

Without seeing exactly how your other event is attached, .triggerHandler() would be my best suggestion, as the event doesn't bubble up for capture by the .Net handler.

Nick Craver
Not working, i think triggerHandler() is the same as trigger() in this issue. The change event generated by .net will be fired in both of them. triggerHandler() will not trigger browser events, but the .net js is not a browser event.
Danny Chen
lol 12 more to go
Anurag
@Danny - Since it's bound directly to the element, you'll want to use the approach here for firing the change event: http://stackoverflow.com/questions/168596/programmatically-triggering-events-in-javascript-for-ie-using-jquery
Nick Craver
@Nick Craver Thank you so much to help me find such useful docs! I'll try their solution!
Danny Chen
@Nick Craver I'm sure now it's a MS bug. I create a web application in both VS2008 and VS2010 with the same code. In 2008 the bug occurs while in 2010 everything is OK.
Danny Chen
@Danny - .Net 4 changed a *lot* of things, this was one of many to cope with situations exactly like this one., if you target .Net 3.5 in Vs 2010 you'll see the same error.
Nick Craver
@Nick Craver Yes I know that, here I say VS2010 means .Net 4 which is integrated. Thanks a lot for your help.
Danny Chen