Link to home
Start Free TrialLog in
Avatar of navinbabu
navinbabuFlag for India

asked on

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;
        }
    </script>

    <title></title>
    </head>
<body>

    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
    </asp:ScriptManager>
    <div>
    
     
                    
                    <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>
                    </td>
                  
        
    
    </div>
    </form>


Index.aspx.vb

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")
        ds.Fill(dts)
        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_from.Items.Add(dr("Name").ToString)me
                'Me.Ddl_from.Items.
                Me.Ddl_City.Items.Insert(dr("Id"), dr("Name").ToString)
            Next
        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
        ds.Fill(dts)
        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/>")
        Next
        Dim a As String = "Abhishek"
        Return a.ToString
    End Function
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of amar31282
amar31282
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial