• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

ASP.NET AJAX callbacks to Web Methods in ASPX pages

Im tryng for auto complete I m including my code . Please check for errors .

Im able to trigger the javascript function but couldnt proceed further .
<script type="text/javascript" language="javascript">
        function getlist() {
            document.getElementById('values').innerHTML = "";
            PageMethods.returnList(document.getElementById('txtinput').value, CallSuccess, CallFailed)
        function CallSuccess(result) {
            document.getElementById('values').innerHTML = "";
            document.getElementById('values').innerHTML = result;
        function CallFailed(res, destCtrl) {
            document.getElementById('values').innerHTML = "";
            document.getElementById('values').innerHTML = res;
        function changetext(val) {
            document.getElementById('txtinput').value = val;
            document.getElementById('txtinput').select = true;


    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
                    <asp:TextBox ID="txtinput" runat="server" Width="385px"></asp:TextBox>
                    <div style="clear:both;"></div>
            <div id="values"  style="position: absolute; z-index:100; vertical-align:top; width: 385px; max-height: 100px; overflow: auto; border: 1px solid #666;"></div>


Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports AjaxControlToolkit
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Script.Services
<System.Web.Script.Services.ScriptService()> _
Partial Class index
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.txtinput.Attributes.Add("onkeyup", "getlist();")

        Dim con As String = ConfigurationManager.ConnectionStrings("food").ConnectionString
        Dim ds As New SqlDataAdapter("SELECT [Name],[Id] FROM [City]", con)
        Dim dts As New DataSet
        Me.Ddl_City.Items.Add("Please Select")
        Dim count As Integer = dts.Tables(0).Rows.Count
        Dim items As Integer = Me.Ddl_City.Items.Count
        items = items - 1
        If items = 0 Then
            For Each dr As DataRow In dts.Tables(0).Rows
                Me.Ddl_City.Items.Insert(dr("Id"), dr("Name").ToString)
        End If
    End Sub
    <WebMethod()> _
           <ScriptMethod()> _
Public Shared Function returnList(ByVal Name As String) As String
        Dim con As String = ConfigurationManager.ConnectionStrings("food").ConnectionString
        Dim ds As New SqlDataAdapter("SELECT [Name],[Id] FROM [City] LIKE @Name", con)
        Dim dts As New DataSet
        Dim out As New StringBuilder
        For Each dr As DataRow In dts.Tables(0).Rows
            out.Append("<a style='cursor:pointer;text-decoration:none;' onmouseover='changetext(this.innerHTML)'>" + dr("Name") + "</a><br/>")
        Dim a As String = "Abhishek"
        Return a.ToString
    End Function
End Class

Open in new window

1 Solution
make sure that you are getting the ellement 'txtinput' inside the javascript ... what i doubt is this that u r should get the element like this document.getElementById(<%=txtinput.ClientID%>').value

If not the issue then check if the control is going to codebehind file by putting one break inside the returnlist method.. if its returning value or not...

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now