JavaScript keeps returning ambigious error (in ASP.NET MVC 2.0)
this is my function (with other lines ive tried/abandoned)...
function DoClicked(eNumber) {
//obj.style = 'bgcolor: maroon';
var eid = 'cat' + eNumber;
//$get(obj).style.backgroundColor = 'maroon';
//var nObj = $get(obj);
var nObj = document.getElementById(eid)
//alert(nObj.getAttribute("style"));
nObj.style.backgroundColor = 'Maroon';
alert(nObj.style.backgroundColor);
//nObj.setAttribute("style", "backgroundcolor: Maroon");
};
This error keeps getting returned even after the last line in the function runs:
Microsoft JScript runtime error: Sys.ArgumentUndefinedException: Value cannot be undefined.
Parameter name: method
this function is called with an "OnSuccess" set in my Ajax.ActionLink call (ASP.NET MVC)... anyone any ideas on this? i have these referenced... even when i remove the 'debug' versions for normal versions, i still get an error but the error just has much less information and s开发者_运维问答ays 'b' is undefined (probably a ms js library internal variable)...
<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
also, this is how i am calling the actionlink method:
Ajax.ActionLink(item.CategoryName, "SubCategoryList", "Home", New With {.CategoryID = item.CategoryID},
New AjaxOptions With {.UpdateTargetId = "SubCat", .HttpMethod = "Post", .OnSuccess = "DoClicked(" & item.CategoryID.ToString & ")"},
New With {.id = "cat" & item.CategoryID.ToString})
well, this was the answer i subsequently found out:
OnSuccess = "function(){DoClicked(" & item.CategoryID.ToString & ");}"
so, when you call your function via OnSuccess or OnComplete, you just need to wrap it inside a "function() {...}"
declaration, and you'll be fine.
Its funny how the vs team doesn't point this out as a 'correct spec' way of doing things, yet it is so picky and ambigious about it when its passed normally. Wasted 4 hours on VS2010 RTM - this is dismal. Surely, this is a bug that they've made no effort to resolve as it's been around for some time now.
I'm guessing something else is bound to a click event, perhaps on the body of the document itself?
Try cancelling the event (you're not passing it in to the function right there, but whatever triggers your click should have access to it).
In jQuery, you want something like:
event.preventDefault();
event.stopPropagation(); // this is the important one
Edit:
You can't just drop them into the function; you'll need to access them according to whatever framework you've got. You're already in a click handler in your code; whatever's triggering that handler should have access to the event object.
For example, in jQuery, you'd do:
$('el').click(function(e){
e.preventDefault();
e.stopPropagation();
alert('stopped');
});
Edit again:
Is your "DoClicked" method being called from an AJAX callback? That is, it's not a "click" event at all?
Edit again #2:
I'm pretty sure I know what's going on, if not why. It's expecting a function reference in the OnSuccess property, which makes sense. I imagine the function gets called with whatever the response from the POST call is, so something like this would work:
OnSuccess = "DoClicked";
function DoClicked(args){
// here, args will (maybe? probably?) represent the response from the POST
}
精彩评论