In this tutorial we will brief you on how to handle DOM events and retrieve event information by using $addHandler() method. This method does has a lot of limitations, for instance it doesn’t enable you to pass additional information to the event handler.
This tutorial will demonstrate how to create Callbacks and Delegates using a ASP.NET 3.5 Web App. C#
ASP.NET AJAX library includes two methods you can use to pass additional information to event handlers:
Function.createCallback(method, context)
Function.createDelegate(instance, method)
When you call the createCallback() method creates a method that passes an additional context parameter to an event handler. This way you can pass anything you want as a context parameter. For instance, the context parameter could be a simple string or it could be a reference to a component.
By calling the createDelegate() method it does not pass additional parameters to the event handler, although, it changes the meaning of this in the handler. Typically, when this is used in an event handler, it refers to the DOM element that raised the event. The createDelegate() method allows you to change this to refer to anything you would like.
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
There are differences between the createCallback() and createDelegate() methods that we will explore in this example below. The page contains four buttons, named btnHandler, btnCallback, btnDelegate, and btnHandlers. Additionally, the page contains four event handlers, named ActionEvent(), ActionEvent2(), ActionEvent3(), ActionEvent4(). We will pass different parameters to these event handlers, depending on how we wire them up to the buttons.
<script type="text/javascript">
function pageLoad()
{
$addHandler($get("btnHandler"), "click", ActionEvent);
var callback = Function.createCallback(ActionEvent2, "An object");
$addHandler($get("btnCallback"), "click", callback);
var delegate = Function.createDelegate("An object", ActionEvent3);
$addHandler($get("btnDelegate"), "click", delegate);
$addHandlers($get("btnHandlers'), {click: ActionEvent4}, "An object");
}
function ActionEvent(event)
{
alert([this.id, event.type]);
}
function ActionEvent2(event, context)
{
alert([this.id, event.type, context]);
}
function ActionEvent3(event)
{
alert([this.id, event.type]);
}
function ActionEvent4(event)
{
alert([this.id, event.type]);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="smScriptManager" runat="server" />
<input id="btnHandler" type="button" value="Handler" />
<input id="btnCallback" type="button" value="Callback" />
<input id="btnDelegate" type="button" value="Delegate" />
<input id="btnHandlers" type="button" value="Handlers" />
</form>
</body>
</html>
|
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
this, refers to the first two buttons when clicked. If you clicked the second two buttons, then this refers to the string “An object” since “An object” was passed to the createDelegate() method and the $addHandlers() shortcut.
With the $addHandlers() shortcut, if you pass a final argument that represents the context, it does the same thing as calling createDelegate() method. The advantage of $addHandlers() is so you can use this shortcut to wire up to multiple methods events at a time.
In real-world situations, you probably won’t change this to refer to a string; you’ll change it to refer to the client-side control or behavior associated with the event handler. Within the event handler, you may want to refer to the properties and methods of the client-side control or behavior associated with the event.