We all know about the jquery Ajax which is a most popular and widely used methods to make server side calls from client side. ASP.NET provides an easier approach to make server side calls from client side using Page Methods.
Below is the demonstration of Page Method
First create server side method which needs to be called from client side
[System.Web.Services.WebMethod]
public static string Display(string firstName, string lastName)
{
string result = "Welcome " + firstName + " " + lastName + ".";
return result;
}
Create function on client side to call the server side method which uses PageMethods
<script type="text/javascript">
function Display() {
var firstName = document.getElementById('<%=txtFirstName.ClientID %>').value;
var lastName = document.getElementById('<%=txtLastName.ClientID %>').value;
PageMethods.Display(firstName, lastName, onSucess, onError);
function onSucess(result) {
alert(result);
}
function onError(result) {
alert('Something wrong.');
}
}
</script>
Include the Script Manager in aspx page and set EnablePageMethods property to true
<form id="form1" runat="server">
<div>
<p>Call Server Side Methods Using Page Method</p>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> </asp:ScriptManager>
First Name: <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<br />
Last Name: <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnDisplay" runat="server" Text="Signup" OnClientClick="Display(); return false;" />
</div>
</form>
0 Comment(s)