Hi,
behaviors are Atlas components that allows to dynamically add/remove client-side functionality from controls. For example, if I want to handle click and keypress events, I could attach the corresponding behaviors to an Atlas control. Then, if I don't want to handle the click event anymore for that control, all I have to do is detach the corresponding behavior from the control. In this way you obtain a control with a dynamic "behavior".
Please check
this article for some details about coding an Atlas behavior.
Regarding your requirements, as bleroy suggested, Atlas has a builtin <clickBehavior /> that you can use to make a control handle the click event. For the keypress event, bleroy itself and Wilco Bauwer have already coded two specific behaviors; you can click
here to get the bleroy's one and
here to get the Wilco's one.
Otherwise, if you need a very basic behavior, similar to the <clickBehavior />, here's my version:
Type.registerNamespace('AtlasExamples.WebUI');
AtlasExamples.WebUI.KeyPressBehavior = function() {
AtlasExamples.WebUI.KeyPressBehavior.initializeBase(this);
// Private members.
var _keypressHandler;
// Events.
this.keypress = this.createEvent();
// Initialize / Dispose.
this.initialize = function() {
AtlasExamples.WebUI.KeyPressBehavior.callBaseMethod(this, 'initialize');
_keypressHandler = Function.createDelegate(this, keypressHandler);
this.control.element.attachEvent('onkeypress', _keypressHandler);
}
this.dispose = function() {
this.control.element.detachEvent('onkeypress', _keypressHandler);
_keypressHandler = null;
AtlasExamples.WebUI.KeyPressBehavior.callBaseMethod(this, 'dispose');
}
// Descriptor.
this.getDescriptor = function() {
var td = AtlasExamples.WebUI.KeyPressBehavior.callBaseMethod(this, 'getDescriptor');
td.addEvent('keypress', true);
return td;
}
// Event Handler.
function keypressHandler() {
this.keypress.invoke(this, Web.EventArgs.Empty);
}
}
Type.registerClass('AtlasExamples.WebUI.KeyPressBehavior', Web.UI.Behavior);
Web.TypeDescriptor.addType('script', 'keyPressBehavior', AtlasExamples.WebUI.KeyPressBehavior);and finally, an example that use the <clickBehavior /> and the <keyPressBehavior /> to solve your problem (just change the script reference in the ScriptManager to the correct path to the behavior file):
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Behaviors Example</title>
<atlas:ScriptManager id="sm" runat="server">
<Scripts>
<atlas:ScriptReference Path="ScriptLibrary/KeyPressBehavior.js" />
</Scripts>
</atlas:ScriptManager>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="myTextBox" runat="server"></asp:TextBox>
</div>
</form>
<script type="text/javascript">
function onTextBoxClick() {
alert('TextBox clicked!');
}
function onTextBoxKeyPress() {
alert('Key pressed in TextBox! KeyCode is ' + window.event.keyCode);
}
</script>
<script type="text/xml-script">
<page>
<components>
<textBox id="myTextBox">
<behaviors>
<clickBehavior click="onTextBoxClick" />
<keyPressBehavior keypress="onTextBoxKeyPress" />
</behaviors>
</textBox>
</components>
</page>
</script>
</body>
</html>