Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegisterStartupScript: Calling AJAX API Issue 1

Status
Not open for further replies.

Vladekins1

Programmer
Jul 29, 2008
2
US
Hi,

I am trying to figure out why when I invoke a JavaScript function that calls an API via a button on my ASP.NET webpage I am able to successfully call my API and get a complete response of all its methods. However if I call the same funtion using the RegisterStartupScript I only get a partial list of methods in the response from the API.

Any comments or suggestions will be greatly appreciated.

Below is the code I am using:

Code:
Dim script As String = String.Format("javascript:geocode({0});", "'" & address & ", " & city & ", " & state & "'")
ClientScript.RegisterStartupScript(Me.GetType(), "geocode", script, True)

 
Hello!

I'm assuming you're trying to implement the ICallbackEventHandler interface on your page. The steps needed are:

1) modify the code behind of your page
Code:
Public Partial Class YOUR_PAGE_CLASS_NAME
    Inherits System.Web.UI.Page
    Implements ICallbackEventHandler
End Class

2) add a private member on your page:
Code:
Private callbackResult As String = String.Empty

3) on Page_Load register the ajax script
Code:
Dim cs as ClientScriptManager = Page.ClientScript

Dim Script as String = String.Format(
	"function geocode(data){{{0};}}",
	cs.GetCallbackEventReference(Me, "data", "receive_geocode", "null", true)
)

cs.RegisterClientScriptBlock(Me.GetType(), "geocode", Script, true)

4) implement the ICallbackEventHandler required methods
Code:
Public Function GetCallbackResult() As String
    Return Me.callbackResult
End Function

Public Sub RaiseCallbackEvent(ByVal eventArgument As String)
    'eventArgument contains data sent from client (ex. a sring containting the address, city and state)
    'do stuff (ex. split eventArgument by "," and then search for the geo code)
    Me.callbackResult = "GEO_CODE"
End Sub

5) in your aspx page define the receiver function and a caller function
Code:
function receive_geocode()
{
	alert(arguments[0]); //this will contain the result from server
}

function call_geocode(address, city, state)
{
	geocode(address + ',' + city + ',' + state);
}

6) use the server call
Code:
<asp:Button id="myButton" runat="server" Text="Show GeoCode" OnClientClick="call_geocode('Some street #1', 'Some city', 'Some state'); return false;"/>
or
<input type="button" value="Show GeoCode" onclick="call_geocode('Some street #1', 'Some city', 'Some state')"/>
or
<a href="javascript:void(0)" onclick="call_geocode('Some street #1', 'Some city', 'Some state')">Show GeoCode</a>

[morning]
 
Implementing the ICallbackEventHandler was the solution for this issue. Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top