This example demonstrates how to apply the jQuery AutoComplete
plugin to a text box editor.
Overview
To apply jQuery selectors to a text box editor, use one of the following approaches:
- Add a ASPxGlobalEvents component to the page and handle its client-side ControlsInitialized event to initialize the text box editor. To access the editor, use its ClientInstanceName property.JavaScript
function OnControlsInitialized(s, e) { $(txt.GetInputElement()).autocomplete({ source: availableTags, position: { my: "left top", at: "left bottom", of: txt.GetMainElement() }, select: function (event, ui) { txt.SetValue(ui.item.value); } }); }
- Handle the editor's client-side
Init
event and use thes
parameter to get the corresponding the client object.JavaScriptfunction OnTextBoxInit(s, e) { $(s.GetInputElement()).autocomplete({ source: availableTags, position: { my: "left top", at: "left bottom", of: s.GetMainElement() }, select: function (event, ui) { s.SetValue(ui.item.value); } }); }
Files to Review
- ControlEvents.aspx (VB: ControlEvents.aspx)
- GlobalEvents.aspx (VB: GlobalEvents.aspx)
- AutoCompleteStyles.css
Documentation
Example Code
ASPx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ControlEvents.aspx.cs" Inherits="ControlEvents" %>
<%@ Register Assembly="DevExpress.Web.v13.1, Version=13.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dx" %>
<!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></title>
<link rel="Stylesheet" type="text/css" href="Styles/AutoCompleteStyles.css" />
<script src="Scripts/jquery-1.6.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
function OnTextBoxInit(s, e) {
//http://jqueryui.com/demos/autocomplete/
//http://docs.jquery.com/UI/Position
$(s.GetInputElement()).autocomplete({
source: availableTags,
position:
{
my: "left top",
at: "left bottom",
of: s.GetMainElement()
},
select: function (event, ui) {
s.SetValue(ui.item.value);
}
});
}
</script>
</head>
<body>
<form id="mainForm" runat="server">
<div>
<dx:ASPxPageControl ID="pageControl" runat="server" ActiveTabIndex="0" EnableCallBacks="true">
<TabPages>
<dx:TabPage Text="Static TabPage">
<ContentCollection>
<dx:ContentControl runat="server">
<b>Click "Dynamic TabPage" to create its content via callback</b>
</dx:ContentControl>
</ContentCollection>
</dx:TabPage>
<dx:TabPage Text="Dynamic TabPage">
<ContentCollection>
<dx:ContentControl runat="server">
<b>Type in, for example, the "a" key to invoke the jQuery AutoComplete plugin</b>
<br />
<br />
<dx:ASPxTextBox ID="textBox" runat="server" Width="170px">
<ClientSideEvents Init="OnTextBoxInit" />
</dx:ASPxTextBox>
</dx:ContentControl>
</ContentCollection>
</dx:TabPage>
</TabPages>
</dx:ASPxPageControl>
</div>
</form>
</body>
</html>
ASPx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GlobalEvents.aspx.cs" Inherits="GlobalEvents" %>
<%@ Register Assembly="DevExpress.Web.v13.1, Version=13.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dx" %>
<!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></title>
<link rel="Stylesheet" type="text/css" href="Styles/AutoCompleteStyles.css" />
<script src="Scripts/jquery-1.6.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
function OnControlsInitialized(s, e) {
//http://jqueryui.com/demos/autocomplete/
//http://docs.jquery.com/UI/Position
$(txt.GetInputElement()).autocomplete({
source: availableTags,
position:
{
my: "left top",
at: "left bottom",
of: txt.GetMainElement()
},
select: function (event, ui) {
txt.SetValue(ui.item.value);
}
});
}
</script>
</head>
<body>
<form id="mainForm" runat="server">
<div>
<b>Type in, for example, the "a" key to invoke the jQuery AutoComplete plugin</b>
<br />
<br />
<dx:ASPxTextBox ID="textBox" runat="server" ClientInstanceName="txt" Width="170px">
</dx:ASPxTextBox>
<dx:ASPxGlobalEvents ID="globalEvents" runat="server">
<ClientSideEvents ControlsInitialized="OnControlsInitialized" />
</dx:ASPxGlobalEvents>
</div>
</form>
</body>
</html>
CSSul.ui-autocomplete
{
border: solid 1px #000000;
list-style-type: none;
margin: 0px;
overflow: auto;
padding: 0px;
width: 168px;
}
ul.ui-autocomplete a.ui-corner-all
{
background-color: #EEEEEE;
display: block;
font-weight: bold;
}
ul.ui-autocomplete a.ui-state-hover
{
background-color: #0A246A;
color: #ffffff;
cursor: pointer;
display: block;
}