|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis sample demonstrate, how you can use AutoCompleteExtender control from ASP.NET AJAX Control Toolkit and customize its behavior without modifying it. BackgroundI like Google's suggest feature and was thinking of using it in any of my project. ASP.NET's AJAX Control Toolkit have a similar control (AutoCompleteExtender) which provides a basic items to complete this functionality. I searched about its usage, and found many examples, but I was not satisfied with them. They all were populating only a single field using this extender. I then came up with an idea, why not fill a contacts detail including its name, email address, and phone numbers using just one extender, but without modifying any provided functionality, so that our code be used with newer versions. Below is what it will look after populating that contact form. Using the codeLets checkout how AutoCompleteExtender works. Below is the description for this on toolkit's sample site.
It says that this control will fetch its data from a webservice, and it can be attached to a TextBox control (it can only be attached with one control). When users starts typing in that TextBox control, it fetches a suggestion list from configured webservice. So we need two things, one would be our sample contact page, and another a webservice. Here is our webservice code. using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Script;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.Services.Protocols;
using AjaxControlToolkit;
///
You will notice some extra adornments on service and web method. You must adorn this service with "ScriptService" attribute, because without it, you can not access it from your client script. [ScriptService()]
public class SuggestionService : System.Web.Services.WebService {
And our webmethod would return list of string items in JSON format, so it also requires [WebMethod]
[ScriptMethod()]
public string[] GetContacts(string prefixText, int count, string contextKey) {}
In addition to that you have also noticed that I have returned this data differently. I have used items.Add(AutoCompleteExtender.CreateAutoCompleteItem(c.FullName, serializer.Serialize(c)));
Now I had another obstacle to overcome, how to pass a complex data for a contact. Here comes JSON for my help. I serialized my contact objects using So far I have bragged that my contact object is a bit complex, here is what it looks like. Below is my ///
Now we have to work out on our contact page. Below is its basic HTML, this contact form contains, firstname, lastname, email address, and phone number fields. Each phone number in-turn have prefix, area code, number and extension fields. <div style="border: 1px solid rgb(204, 204, 204); padding: 10px; width: 400px;">
<table cellspacing="0" cellpadding="0" border="0">
<tbody><tr>
<td style="width: 100px; text-align: right;">First Name:</td>
<td><asp:textbox runat="server" id="txtFirstName" /></td>
</tr>
<tr>
<td style="width: 100px; text-align: right;">Last Name:</td>
<td><asp:textbox runat="server" id="txtLastName" /></td>
</tr>
<tr>
<td style="width: 100px; text-align: right;">Home Phone:</td>
<td>
<table cellspacing="0" cellpadding="0" border="0">
<tbody><tr>
<td><asp:textbox columns="3" runat="server" id="txtHPAreaCode" /></td>
<td>-<asp:textbox columns="3" runat="server" id="txtHPPrefix" /></td>
<td>-<asp:textbox columns="3" runat="server" id="txtHPNumber" /></td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td style="width: 100px; text-align: right;">Work Phone:</td>
<td>
<table cellspacing="0" cellpadding="0" border="0">
<tbody><tr>
<td><asp:textbox columns="3" runat="server" id="txtWPAreaCode" /></td>
<td>-<asp:textbox columns="3" runat="server" id="txtWPPrefix" /></td>
<td>-<asp:textbox columns="3" runat="server" id="txtWPNumber" /></td>
<td> x </td>
<td><asp:textbox columns="3" runat="server" id="txtWPExtension" /></td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td style="width: 100px; text-align: right;">Email Address :</td>
<td><asp:textbox runat="server" id="txtEmail" /></td>
</tr>
</tbody></table>
</div>
To use First specify the This completes our basic setup. If you test this page now, you will get a suggestion list after typing two letters in firstname field, and when you select any item from that list, only firstname field is set with selected name. All other fields would remain blank. Now we need to add our magic code, which will enable us to achieve our goal. <script type="text/javascript">
function OnContactSelected(source, eventArgs)
{
var results = eval('(' + eventArgs.get_value() + ')');
$get('txtFirstName').value = results.FirstName;
$get('txtLastName').value = results.LastName;
if (results.HomePhone.AreaCode != null)
$get('txtHPAreaCode').value = results.HomePhone.AreaCode;
if (results.HomePhone.Prefix != null)
$get('txtHPPrefix').value = results.HomePhone.Prefix;
if (results.HomePhone.Number != null)
$get('txtHPNumber').value = results.HomePhone.Number;
if (results.WorkPhone.AreaCode != null)
$get('txtWPAreaCode').value = results.WorkPhone.AreaCode;
if (results.WorkPhone.Prefix != null)
$get('txtWPPrefix').value = results.WorkPhone.Prefix;
if (results.WorkPhone.Number != null)
$get('txtWPNumber').value = results.WorkPhone.Number;
if (results.WorkPhone.Number != null)
$get('txtWPExtension').value = results.WorkPhone.Extension;
if (results.Email != null)
$get('txtEmail').value = results.Email;
}
</script>
But before that we need to add some client-side behavior to our extender, we need to set This conclude our sample, now you can test it yourself. History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||