62,074
社区成员
发帖
与我相关
我的任务
分享
public partial class _Default : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
string ICallbackEventHandler.GetCallbackResult()
{
// System.Threading.Thread.Sleep(2000);
return CallBackValue + ", C World!";
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
this.CallBackValue = eventArgument;
}
<script type="text/javascript">
function CallServer() // 发送回调
{
var arg = "Cognizant";
<%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", null)%>;
}
function ReceiveServerData(alertContent) // 接收回调
{
alert(alertContent);
}
</script>
<%@ 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 runat="server">
<title>Without Refresh Demo</title>
<script type="text/javascript">
function CallServer()
{
var arg = "Cognizant";
<%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", null)%>;
}
function ReceiveServerData(alertContent)
{
alert(alertContent);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<button onclick="CallServer()">HTML Button CallServer</button>
</div>
<div>
<asp:Button ID = "btn1" runat = "server" Text = "ASP:Button" />
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
btn1.Attributes.Add("onclick", "CallServer();return false;");
}
private string CallBackValue = string.Empty;
#region ICallbackEventHandler Members
string ICallbackEventHandler.GetCallbackResult()
{
// System.Threading.Thread.Sleep(2000);
return CallBackValue + ", C World!";
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
this.CallBackValue = eventArgument;
}
#endregion
}
ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", null)
非常重要function CallServer()
{
var arg = "Cognizant";// 呵呵 违反了保密原则?? 开玩笑, 一个 单词而已嘛!
<%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", null)%>;
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
this.CallBackValue = eventArgument;
}
string ICallbackEventHandler.GetCallbackResult()
{
// System.Threading.Thread.Sleep(2000);
return CallBackValue + ", C World!";
}
<script type="text/javascript">
//...
function ReceiveServerData(alertContent)
{
alert(alertContent);
}
</script>