Calling a Static Page Method using jQuery
If you do not plan to call a web method from multiple pages, don’t perform all the work of creating a separate web service. Instead, you can expose a static method from the same AJAX page calling the web method.
1..ASPX Page -
2. Code Behind Code (.ASPX.CS)
Add this method
1..ASPX Page -
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 id="Head1" runat="server">
<title>Show Web Service Methodtitle>
<script type="text/javascript"
src="../Scripts/jquery-1.4.1.js">script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnGet").click(function () {
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "ShowPageMethod.aspx/GetQuote",
success: function (data) {
$("#spanQuote").html(data.d);
},
error: function () {
alert("The call to the web service failed.");
}
})
});
});
script>
head>
<body>
<form id="form1" runat="server">
<div>
<input id="btnGet" type="button"
value="Get
Quote" />
<br />
<br />
<span id="spanQuote">span>
div>
form>
body>
html>
2. Code Behind Code (.ASPX.CS)
Add this method
[System.Web.Services.WebMethod]
public static string GetQuote()
{
List<string>
quotes = new List<string>();
quotes.Add("The fool who is silent
passes for wise.");
quotes.Add("The early bird catches the
worm.");
quotes.Add("If wishes were true, shepherds
would be kings.");
Random rnd = new Random();
return quotes[rnd.Next(quotes.Count)];
}
Don´t Work!!!!you lie
ReplyDeleteVery nice this blog . this blog is very informative.such a great blogging website.
ReplyDelete"woman wine expert in usa
"