Get a reference to the calling object in a Javascript ASP.NET Ajax callback function?
I am calling an ASP.NET Ajax met开发者_开发知识库hod (e.g. a method tagged with [Ajax.AjaxMethod]
) on a control which is dynamically created in JavaScript when the user clicks a link (basically it is inside a table, and when the link is clicked a tr/td is built out in JS code and added to the table). In the callback function I need to know what row is being called since I'm appending data to an input control in that row only; I have a hidden field that contains the total number of items added, but I cannot use this as it always adds to the last row (since it is incremented when a new row is added) not the row which is firing the function.
Something like the following:
// fieldName is a lookup string...
function loadOperators(fieldName) {
MyPage.LoadOperatorsFor(fieldName, loadOperators_Callback);
}
function loadOperators_Callback(response) {
var currentRow = // how to obtain this?
// do other stuff here...
}
I am also using jQuery but the methods are done in ASP.NET AJAX style as that's the current style we use at work.
How am I able to determine the actual row which is firing the Ajax function, so I only populate the dropdown list contained in that row (I cannot rely on the user creating a row, and then populating that row; they might create say 5 rows and then go back to Row #2 and select something, so I would have to know they chose a value in Row #2 and not 3/4/5)? It doesn't seem to be part of the response object, but the Callback function doesn't seem to take any additional parameters.
The only thing I can think of doing would be to add it to my AjaxMethod so it would be passed back in the response, but this seems like a hack at best since I return a DataSet and the row number has no bearing on the data.
I forgot to add, I am not using the ASP.NET AJAX library, only jQuery and tagging methods with [Ajax.AjaxMethod]
You need pass a delegate as your call back parameter
function loadOperators(fieldName) {
MyPage.LoadOperatorsFor(fieldName, Function.createDelegate({"fieldName":fieldName},loadOperators_Callback));
}
and in your callback you can
function loadOperators_Callback(response) {
var currentRow = this.fieldName;
// do other stuff here...
}
All ASP.NET AJAX web service javascript proxies allow you to pass in a context like so:
MyPage.LoadOperatorsFor(fieldName, loadOperators_CallBack,
loadOperators_Error, context);
The loadOperators_Error
is your error handler, and context
can be anything you want - in your case, pass in the currentRow.
Then on your response, you'll have:
function loadOperators_CallBack(response, context) {
// context is your currentRow value
}
// fieldName is a lookup string...
function loadOperators(fieldName) {
// assume this is a click handler on the row
// if it isn't somehow get the row.
var row = $(this);
MyPage.LoadOperatorsFor(fieldName, loadOperators_Callback.bind(row));
}
function loadOperators_Callback(response) {
var currentRow = this;
// do other stuff here...
}
Get the row in your loadOperators function then bind the this
scope to the row in your callback. You may need to emulate Function.prototype.bind
in IE<9. I recommend _.bind
for the emulation.
Actaully you can use $.proxy(row, loadOperators_Callback)
for this.
For the record I was able to solve this by simply using an inline anonymous function so I can reference the lineNumber from the calling method, instead of passing a function reference. Simply and effective.
精彩评论