kdeutsch
asked on
get service from javascript function
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: <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
ASKER
Ok changed this, but now its giving me an error on my javscript on the sendrequest.
<asp:Content ID="Content1" ContentPlaceHolderID="Head Content" Runat="Server">
<script language="javascript" type="text/javascript">
function SendRequest() {
AutoComplete.FindName('<%= txtAdmin.C lientID%>' ).value
function OnComplete(arg) {
alert(arg)
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Body Content" Runat="Server">
<table width="100%">
<tr>
<td style="height:20px"></td>
</tr>
<tr>
<td align="center">Add Admin: <a sp:TextBox ID="txtAdmin" runat="server" Width="260" AutoPostBack="true"
AutoCompleteType="DisplayN ame" />
<asp:AutoCompleteExtender ID="txtAdmin_AutoCompleteE xtender" runat="server" Enabled="True" TargetControlID="txtAdmin"
ServiceMethod="FindName" MinimumPrefixLength="0" />
<asp:TextBoxWatermarkExten der ID="txtAdmin_TextBoxWaterm arkExtende r" runat="server" Enabled="True" TargetControlID="txtAdmin"
WatermarkText="Enter LastName First"/>
</td>
</tr>
</table>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="Head
<script language="javascript" type="text/javascript">
function SendRequest() {
AutoComplete.FindName('<%=
function OnComplete(arg) {
alert(arg)
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Body
<table width="100%">
<tr>
<td style="height:20px"></td>
</tr>
<tr>
<td align="center">Add Admin: <a
AutoCompleteType="DisplayN
<asp:AutoCompleteExtender ID="txtAdmin_AutoCompleteE
ServiceMethod="FindName" MinimumPrefixLength="0" />
<asp:TextBoxWatermarkExten
WatermarkText="Enter LastName First"/>
</td>
</tr>
</table>
</asp:Content>
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.
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.
ASKER
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("o nKeyup", "SendRequest()")
End Sub
HTML
<asp:Content ID="Content1" ContentPlaceHolderID="Head Content" Runat="Server">
<script language="javascript" type="text/javascript">
function SendRequest() {
AutoComplete.FindName('<%= txtAdmin.C lientID%>' ).value
function OnComplete(arg) {
alert(arg)
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Body Content" Runat="Server">
<table width="100%">
<tr>
<td style="height:20px"></td>
</tr>
<tr>
<td align="center">Add Admin: <a sp:TextBox ID="txtAdmin" runat="server" Width="260" AutoPostBack="true"
AutoCompleteType="DisplayN ame" />
<asp:AutoCompleteExtender ID="txtAdmin_AutoCompleteE xtender" runat="server" Enabled="True" TargetControlID="txtAdmin"
ServiceMethod="FindName" MinimumPrefixLength="0" />
<asp:TextBoxWatermarkExten der ID="txtAdmin_TextBoxWaterm arkExtende r" runat="server" Enabled="True" TargetControlID="txtAdmin"
WatermarkText="Enter LastName First"/>
</td>
</tr>
</table>
</asp:Content>
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("o
End Sub
HTML
<asp:Content ID="Content1" ContentPlaceHolderID="Head
<script language="javascript" type="text/javascript">
function SendRequest() {
AutoComplete.FindName('<%=
function OnComplete(arg) {
alert(arg)
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Body
<table width="100%">
<tr>
<td style="height:20px"></td>
</tr>
<tr>
<td align="center">Add Admin: <a
AutoCompleteType="DisplayN
<asp:AutoCompleteExtender ID="txtAdmin_AutoCompleteE
ServiceMethod="FindName" MinimumPrefixLength="0" />
<asp:TextBoxWatermarkExten
WatermarkText="Enter LastName First"/>
</td>
</tr>
</table>
</asp:Content>
ASKER
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.Protoc ols
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.Service s.ScriptSe rvice()> _
<WebService(Namespace:="http://tempuri.org/") > _
<WebServiceBinding(Conform sTo:=WsiPr ofiles.Bas icProfile1 _1)> _
<Global.Microsoft.VisualBa sic.Compil erServices .DesignerG enerated() > _
Public Class AutoComplete
Inherits System.Web.Services.WebSer vice
<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
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protoc
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.Service
<WebService(Namespace:="http://tempuri.org/")
<WebServiceBinding(Conform
<Global.Microsoft.VisualBa
Public Class AutoComplete
Inherits System.Web.Services.WebSer
<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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
thanks
txtAdmin.Attributes.Add("o
to
txtAdmin.Attributes.Add("o
Note: Your javascript code is not fully implemented yet, but I suppose you want the above part working.