One Time Event Handlers using jQuery and ASP.NET
Most of the times the JavaScript event handlers attached with an element fire
every time the event under consideration is raised. For example, if you wire a
click event handler to the click event of a button then clicking that button
will invoke the event handler function every time. At times, however, this
behavior is undesirable.
Suppose that you have a hyperlink or a button and clicking on that element
causes an Ajax request to be sent to the server. The Ajax request returns some
data that is loaded in some other element. Once the data is retrieved there is
no need for the click event handler to fire. If you keep firing the click event
handler you would be unnecessary making Ajax requests to the server.
One way to avoid these unwanted Ajax requests is to maintain a flag that
tells you whether data for an element has already been retrieved or not.
However, this may slightly complicate the JavaScript code. As an alternative you
can unsubscribe the click event handler when it gets executed the first time.
That means you need to create event handlers that fire only one time. Luckily,
jQuery provides an inbuilt way to accomplish this task - one() method.
To understand how the one() method is used you will create an ASP.NET Web
Form as shown below:

As you can see the above Web Form consists of a GridView control that lists
EmployeeID, FirstName and LastName from the Employees table of Northwind
database. The Get Notes column is a a HyperLinkField and doesn't have any server
side functionality. Clicking on the Get Notes link executes some Ajax code that
retrieves Notes for an employee. The Notes are appended to a Label control
placed below the GridView. Once retrieved there is no need to retrieve Notes of
the same employee again and hence the click event handler of the Get Notes link
for that employee can be removed.
To develop this example, create an empty ASP.NET Web Forms application. Then
add the ADO.NET Entity Framework Data Model for the Employees table. The
following figure shows how the Employee entity class looks like:

Then add a Web Form to the project and place a GridView on it. Add two
BoundField columns and one HyperLinkField column to the GridView and design it
as shown in the beginning of this article. Also, place a Label control
below the GridView.
Now, set the SelectMethod property of the GridView to GridView1_GetData and
add the GridView1_GetData() method in the code behind. This method does the job
of fetching employee records from the database and is shown below:
public IQueryable GridView1_GetData()
{
NorthwindEntities db=new NorthwindEntities();
var query = from emp in db.Employees
where emp.Country=="USA"
orderby emp.EmployeeID
select new { EmployeeID=emp.EmployeeID,
FirstName=emp.FirstName,
LastName=emp.LastName };
return query;
}
The GridView1_GetData() returns all the Employee records where Country is
USA. Only the EmployeeID, FirstName and LastName columns are returns because our
example needs only these columns.
Now, add a [WebMethod] named GetNotes() as shown below:
[WebMethod]
public static string GetNotes(int employeeid)
{
NorthwindEntities db = new NorthwindEntities();
var query = from emp in db.Employees
where emp.EmployeeID == employeeid
select emp.Notes;
return query.SingleOrDefault().ToString();
}
The GetNotes() web method is intended to be called from the client side
script using Ajax. The GetNotes() method accepts an EmployeeID and returns the
Notes for that employee.
Next, add a <script> reference to jQuery library and also add a <script>
block in the head section of the Web Form. Key in the following jQuery code in
the <script> block:
$(document).ready(function () {
$("a").one("click", function (evt) {
var empId = $(evt.target).closest("tr").children(":first-child").text();
$.ajax({
url: "WebForm1.aspx/getnotes",
type: "POST",
data: JSON.stringify({ employeeid: empId }),
dataType: "json",
contentType: "application/json",
success: function (result) {
$("#lblNotes").append(result.d + "<hr />");
},
error: function () { alert('error'); }
});
});
});
The above code uses one() method of jQuery to wire a one time event handler
to all the hyperlink elements from the Web Form. In our case this will cause all
Get Notes links to have the specified function as the one time click event
handler. The first parameter of the one() method is the type of event that is to
be handled (click in this case). The second parameter is the event handler
function. The event handler function retrieves the EmployeeID from the first
column of the row whose Get Notes link is clicked. Notice the use of closest(),
children() methods and :first-child selector to accomplish this task.
Then an Ajax call is made to the GetNotes() web method using jQuery .$ajax().
The EmployeeID retrieved earlier is passed as the data parameter. The success
function receives the return value of the web method. In this case the success
function will receive the Notes for that employee. The Notes are then appended
in the Label (lblNotes) using append() method.
To test the function of the Web Form, set a breakpoint at the GetNotes() web
method and run the application. You will observe that when you click on the Get
Notes link for an employee for the first time, the Ajax call is made and the
Notes are retrieved from the database. This will be evident from the fact that
your execution will halt at the breakpoint. Once Notes for an Employee are
retrieved clicking on Get Notes doesn't cause the Ajax call and the web method
won't be called again and again. That's what we wanted!
That's it! Keep coding.