Pages

Tuesday, August 20, 2013

AutoTextbox using ASP.net and Jquery

Auto Suggestion Textbox:
The textbox shows the suggestions simultaneously with the letters you type in it. This uses the ajax call to the Webmethod to retrieve the results for the text you type in the textbox.

Output:

AutoTextbox.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AutoTextbox.aspx.cs" Inherits="Validation.AutoTextbox" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">   
    <script type="text/javascript" src="https://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>   
    <link rel="stylesheet" href="Scripts/jquery-ui-1.10.3.custom/development-bundlethemesbase/jquery-ui.css" />   
    <link rel="stylesheet" href="Scripts/jquery-ui-1.10.3.custom/development-bundle/themes/base/jquery.ui.all.css" />
    <title></title>   
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {           
            $('#txtMemberName').autocomplete({
               
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "AutoTextbox.aspx/GetUserNames",
                        data: "{'keywords':'" + $('#txtMemberName').val() + "'}",
                        dataType: "json",                        
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (result) {
                            alert("Error");                           
                        }
                    });
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div class="ui-widget">
            <asp:TextBox ID="txtMemberName" runat="server" Style="padding: 5px; width: 400px; font: 11pt Verdana;"></asp:TextBox>
        </div>
    </form>
</body>

</html>

AutoTextbox.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Validation
{
    public partial class AutoTextbox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static List<string> GetUserNames(string keywords)
        {
            try
            {
                var Names = new List<string> { "Harsha", "Harry", "Ha", "Babu", "Baba" };
                var result = from n in Names where n.StartsWith(keywords) select n;
                return result.ToList();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return null;
            }
        }
    }
}

No comments:

Post a Comment