Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

get service from javascript function

Posted on 2011-03-07
8
Medium Priority
?
322 Views
Last Modified: 2012-06-21
I am trying to design a service that works with the onkeyup instead of the regular asp.textbox changed event.  the following is what I have but I do not know how to conenct the javascript function to the service and get the textbox information.
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
    <script language="javascript" type="text/javascript">
        function getnames() {
            var theName = document.getElementById("txtadmin")
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="BodyContent" Runat="Server">
   
    <table width="100%">
        <tr>
            <td style="height:20px"></td>
        </tr>
        <tr>
            <td align="center">Add Admin:&nbsp;&nbsp;&nbsp;<asp:TextBox ID="txtAdmin" runat="server" Width="260" AutoPostBack="true" 
            AutoCompleteType="DisplayName" onkeyup="javascript:getNames();" />
            <asp:AutoCompleteExtender ID="txtAdmin_AutoCompleteExtender" runat="server" Enabled="True" TargetControlID="txtAdmin" 
            ServiceMethod="FindName" MinimumPrefixLength="0" />
            <asp:TextBoxWatermarkExtender ID="txtAdmin_TextBoxWatermarkExtender" runat="server" Enabled="True" TargetControlID="txtAdmin" 
            WatermarkText="Enter LastName First"/>
            </td>
        </tr>
    </table>
   
</asp:Content>



Partial Class Admin_test
    Inherits System.Web.UI.Page
    <System.Web.Services.WebMethod()> _
    <System.Web.Script.Services.ScriptMethod()> _
    Public Shared Function Findname(ByVal prefixText As String, ByVal count As String) As String()
        Return New AutoComplete().FindName(prefixText, count)
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtAdmin.Attributes.Add("onKeyup", "javascript(getnames();")
    End Sub
End Class

Open in new window

0
Comment
Question by:kdeutsch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 35062321
Change:
txtAdmin.Attributes.Add("onKeyup", "javascript(getnames();"
to
txtAdmin.Attributes.Add("onKeyup", "getnames()")

Note: Your javascript code is not fully implemented yet, but I suppose you want the above part working.

0
 

Author Comment

by:kdeutsch
ID: 35070010
Ok changed this, but now its giving me an error on my javscript on the sendrequest.

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
    <script language="javascript" type="text/javascript">
        function SendRequest() {
            AutoComplete.FindName('<%=txtAdmin.ClientID%>').value

        function OnComplete(arg) {
            alert(arg)
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="BodyContent" Runat="Server">
   
    <table width="100%">
        <tr>
            <td style="height:20px"></td>
        </tr>
        <tr>
            <td align="center">Add Admin:&nbsp;&nbsp;&nbsp;<asp:TextBox ID="txtAdmin" runat="server" Width="260" AutoPostBack="true"
            AutoCompleteType="DisplayName" />
            <asp:AutoCompleteExtender ID="txtAdmin_AutoCompleteExtender" runat="server" Enabled="True" TargetControlID="txtAdmin"
            ServiceMethod="FindName" MinimumPrefixLength="0" />
            <asp:TextBoxWatermarkExtender ID="txtAdmin_TextBoxWatermarkExtender" runat="server" Enabled="True" TargetControlID="txtAdmin"
            WatermarkText="Enter LastName First"/>
            </td>
        </tr>
    </table>
   
</asp:Content>
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 35074245
Glad to solve your question.
Regarding your post question on js implementation. Please post the exact error message and any reference code that you are using to implement this service.
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:kdeutsch
ID: 35085540
Hi,

It throws an error when i start to type in the textbox and gives me
Object expected and it highlights the SendRequest of the onkeyup

this is my code behind
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtAdmin.Attributes.Add("onKeyup", "SendRequest()")
    End Sub


HTML
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
    <script language="javascript" type="text/javascript">
        function SendRequest() {
            AutoComplete.FindName('<%=txtAdmin.ClientID%>').value

        function OnComplete(arg) {
            alert(arg)
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="BodyContent" Runat="Server">
   
    <table width="100%">
        <tr>
            <td style="height:20px"></td>
        </tr>
        <tr>
            <td align="center">Add Admin:&nbsp;&nbsp;&nbsp;<asp:TextBox ID="txtAdmin" runat="server" Width="260" AutoPostBack="true"
            AutoCompleteType="DisplayName" />
            <asp:AutoCompleteExtender ID="txtAdmin_AutoCompleteExtender" runat="server" Enabled="True" TargetControlID="txtAdmin"
            ServiceMethod="FindName" MinimumPrefixLength="0" />
            <asp:TextBoxWatermarkExtender ID="txtAdmin_TextBoxWatermarkExtender" runat="server" Enabled="True" TargetControlID="txtAdmin"
            WatermarkText="Enter LastName First"/>
            </td>
        </tr>
    </table>
   
</asp:Content>
0
 

Author Comment

by:kdeutsch
ID: 35085561
The service sits on a code behind page in my APp_code folder called autoComplete.vb which goes through a autocomplete.asmx file.

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports ReadinessCommon
Imports System.Data
Imports System.Data.SqlClient


' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class AutoComplete
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function FindName(ByVal prefixText As String, ByVal count As String) As String()
        Dim items As New List(Of String)

        sql = "SELECT strFullname FROM WorsDotNet.dbo.tblUser WHERE strFullName like '%" & prefixText & "%' ORDER BY strFullName ASC"

        myDataTable = New DataTable
        myDataTable = getData(sql)

        If myDataTable.Rows.Count > 0 Then
            For Each dRow As DataRow In myDataTable.Rows
                items.Add(dRow(0).ToString)
            Next
        End If

        Return items.ToArray
    End Function
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 2000 total points
ID: 35090151
Uhmm, I will change code as follows:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtAdmin.Attributes.Add("onKeyup", "SendRequest(this)")
    End Sub


HTML
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
    <script language="javascript" type="text/javascript">
        function SendRequest(txtObj) {
           var results = AutoComplete.FindName(txtObj.id);
//Consume results if (results != null) then use it.
        }
    </script>
</asp:Content>
//All other code remain the same

Note: I notice that you are missing the scriptmanager in your html markup
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods = "true">
Any AJAX page requires it, otherwise js may not work properly.

0
 

Author Comment

by:kdeutsch
ID: 35094578
mas_oz2003:

The script manager sits in my sitemaster along with all the service requests, its easier than to put on every page.

Ok getting a bunch more errors, but the errors are ones I think I can handle.
0
 

Author Closing Comment

by:kdeutsch
ID: 35094594
thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question