=============================== Aspx Page Source ============================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 id="Head1" runat="server">
<title>:: Using PageMethods :: </title>
<style type="text/css">
#MessagePlaceholder
{
background-color: #C0C0C0;
}
</style>
</head>
<body>
<script language="javascript" type="text/javascript">
// Step 4: Use the PageMethod from code
function getServerGreeting() {
$get("MessagePlaceholder").innerHTML =
"<img alt=\"Please Wait\" src=\"image\\ajaxwait01.gif\" />";
// NOTE: Instead of using getElementById in the last
// statement, ASP.AJAX has a shortcut to this method
// that does the same thing, $get.
// Call the server method
PageMethods.GetGreeting(getServerGreeting_Success);
return false;
}
// Step 5: Process the callback
function getServerGreeting_Success(result) {
/// <summary>
/// This is the callback method to handle the
/// result from the server.
/// </summary>
/// <param name="result">The result from the server</param>
$get("MessagePlaceholder").innerHTML = result;
$get("<%= Label1.ClientID %>").innerText = 'on Label1 '+ result;
$get("<%= TextBox1.ClientID %>").value = 'on TextBox1 '+ result;
}
</script>
<form id="form1" runat="server">
<div style="text-align:center; width:200px;">
<!-- Step 1: Add a ScriptManager to the page -->
<!-- Step 2: Set the EnablePageMethods property to true -->
<asp:scriptmanager id="sm" runat="server" enablepagemethods="true" />
<input type="button" value="Get Message Html Btn" onclick=" return getServerGreeting();"
style="width:145px;" /><br />
<asp:Button ID="Button1" runat="server" Text="Asp Button with Page Method"
Width="192px" onclientclick=" return getServerGreeting();" />
<br />
<asp:Button ID="Button2" runat="server"
Text="Asp Button with without Page Method" onclick="Button2_Click" /><br />
<asp:Label ID="Label1" runat="server" Text="Label" BackColor="#B6C8ED"></asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server" Width="340px"></asp:TextBox>
<br />
<div id="MessagePlaceholder"></div>
</div>
</form>
</body>
</html>
=============================== C# Page Code =================================
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services; /// new add for webMethod
using System.Threading;/// for threading purpose (optional) if
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("On PageLoad: " + DateTime.Now.ToLongTimeString());
}
[WebMethod]
public static string GetGreeting()
{
// Step 3: Create a static method and decorate it with
// the WebMethod attribute
// In order to slow the method down a bit to illustrate
// the async call, have the method wait 2 seconds.
Thread.Sleep(2000); //optional
// Return the result
return "Hello PageMethods: "+DateTime.Now.ToLongTimeString();
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write(" On Button Click : " + DateTime.Now.ToLongTimeString());
TextBox1.Text = " On Button Click : " + DateTime.Now.ToLongTimeString();
Label1.Text = " On Button Click : " + DateTime.Now.ToLongTimeString();
}
}
useful link
http://blogs.visoftinc.com/2008/09/07/asp-net-ajax-page-methods/
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 id="Head1" runat="server">
<title>:: Using PageMethods :: </title>
<style type="text/css">
#MessagePlaceholder
{
background-color: #C0C0C0;
}
</style>
</head>
<body>
<script language="javascript" type="text/javascript">
// Step 4: Use the PageMethod from code
function getServerGreeting() {
$get("MessagePlaceholder").innerHTML =
"<img alt=\"Please Wait\" src=\"image\\ajaxwait01.gif\" />";
// NOTE: Instead of using getElementById in the last
// statement, ASP.AJAX has a shortcut to this method
// that does the same thing, $get.
// Call the server method
PageMethods.GetGreeting(getServerGreeting_Success);
return false;
}
// Step 5: Process the callback
function getServerGreeting_Success(result) {
/// <summary>
/// This is the callback method to handle the
/// result from the server.
/// </summary>
/// <param name="result">The result from the server</param>
$get("MessagePlaceholder").innerHTML = result;
$get("<%= Label1.ClientID %>").innerText = 'on Label1 '+ result;
$get("<%= TextBox1.ClientID %>").value = 'on TextBox1 '+ result;
}
</script>
<form id="form1" runat="server">
<div style="text-align:center; width:200px;">
<!-- Step 1: Add a ScriptManager to the page -->
<!-- Step 2: Set the EnablePageMethods property to true -->
<asp:scriptmanager id="sm" runat="server" enablepagemethods="true" />
<input type="button" value="Get Message Html Btn" onclick=" return getServerGreeting();"
style="width:145px;" /><br />
<asp:Button ID="Button1" runat="server" Text="Asp Button with Page Method"
Width="192px" onclientclick=" return getServerGreeting();" />
<br />
<asp:Button ID="Button2" runat="server"
Text="Asp Button with without Page Method" onclick="Button2_Click" /><br />
<asp:Label ID="Label1" runat="server" Text="Label" BackColor="#B6C8ED"></asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server" Width="340px"></asp:TextBox>
<br />
<div id="MessagePlaceholder"></div>
</div>
</form>
</body>
</html>
=============================== C# Page Code =================================
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services; /// new add for webMethod
using System.Threading;/// for threading purpose (optional) if
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("On PageLoad: " + DateTime.Now.ToLongTimeString());
}
[WebMethod]
public static string GetGreeting()
{
// Step 3: Create a static method and decorate it with
// the WebMethod attribute
// In order to slow the method down a bit to illustrate
// the async call, have the method wait 2 seconds.
Thread.Sleep(2000); //optional
// Return the result
return "Hello PageMethods: "+DateTime.Now.ToLongTimeString();
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write(" On Button Click : " + DateTime.Now.ToLongTimeString());
TextBox1.Text = " On Button Click : " + DateTime.Now.ToLongTimeString();
Label1.Text = " On Button Click : " + DateTime.Now.ToLongTimeString();
}
}
useful link
http://blogs.visoftinc.com/2008/09/07/asp-net-ajax-page-methods/
No comments:
Post a Comment