Tuesday, January 1, 2008

ASP.NET AJAX Page Methods (2) Demo


<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Default.aspx.vb" Inherits="PageMethodsDemo._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Untitled Page</title>

<script type="text/javascript">

Function Add()

{

var x = $get("txtX").value;

var y = $get("txtY").value;

PageMethods.AddNumbers(x,y,OnSucceeded,OnFailed);

}

function OnSucceeded(result) {

$get("lblResult").innerHTML = result;

}

function OnFailed(error) {

// Alert user to the error.

alert(error.get_message());

}

</script>

</head>

<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

<div>

<asp:TextBox ID="txtX" runat="server"></asp:TextBox>

+

<asp:TextBox ID="txtY" runat="server"></asp:TextBox>

=

<asp:Label ID="lblResult" runat="server"></asp:Label><br />

<asp:Button ID="btnAdd" runat="server" OnClientClick="Add(); return false;" Text="Add" />

</div>

</form>

</body>

</html>



Imports System.Web.Services

Partial Public Class _Default

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

<WebMethod()> _

Public Shared Function AddNumbers(ByVal x As Integer, ByVal y As Integer) As Integer

If Not IsNumeric(x) AndAlso Not IsNumeric(y) Then

Throw New Exception()

End If

Return x + y

End Function

End Class

blog comments powered by Disqus