1.首先繼承 ICallbackEventHandler
2.在Page_Load 裡面
ClientScriptManager scriptManager = this.Page.ClientScript;
string eventRef = scriptManager.GetCallbackEventReference(this, "argVal", "OnCallComplete", "ctxVal", "OnErrorOccurs", true);
3.Server:
/// <summary>
/// 接受客戶端的參數 進行運算
/// </summary>
/// <param name="eventArgument"></param>
public void RaiseCallbackEvent(string eventArgument)
{
strCallResult = voidtt();
}
/// <summary>
/// 返回CallBack的結果
/// </summary>
/// <returns></returns>
public string GetCallbackResult()
{
return strCallResult;
}
4.For Example:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var value_1="";
function tt()
{
//doOther();
}
function ProcessCallBackError(arg, context)
{
alert('error!');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<span id="Message_1"></span>
<input type="button" value="tt" onclick="callServerData(value_1,tt)" />
</div>
</form>
</body>
</html>
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var value_1="";
function tt()
{
//doOther();
}
function ProcessCallBackError(arg, context)
{
alert('error!');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<span id="Message_1"></span>
<input type="button" value="tt" onclick="callServerData(value_1,tt)" />
</div>
</form>
</body>
</html>
public string outputCount = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ }
ClientScriptManager csm = Page.ClientScript;
StringBuilder context = new StringBuilder();
context.Append("function ReturnServerValue(arg, context)");
context.Append("{");
context.Append("Message_1.innerText = arg; ");
context.Append("value_1 = arg;");
context.Append("}");
String callbackReference = csm.GetCallbackEventReference(this, "arg", "ReturnServerValue", context.ToString());
String callbackspt = "function callServerData(arg,context) {" + callbackReference + ";}";
csm.RegisterClientScriptBlock(this.GetType(), "callServerData",callbackspt,true);
}
/// <summary>
/// 接受客戶端傳回的參數
/// </summary>
/// <param name="eventArgument"></param>
public void RaiseCallbackEvent(String eventArgument)
{
try
{
outputCount = eventArgument.ToString();
}
catch
{
outputCount = "output";
}
}
/// <summary>
/// Define method that returns callback result.(將運算好的結果返回)
/// </summary>
/// <returns></returns>
public string GetCallbackResult()
{
return outputCount;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ }
ClientScriptManager csm = Page.ClientScript;
StringBuilder context = new StringBuilder();
context.Append("function ReturnServerValue(arg, context)");
context.Append("{");
context.Append("Message_1.innerText = arg; ");
context.Append("value_1 = arg;");
context.Append("}");
String callbackReference = csm.GetCallbackEventReference(this, "arg", "ReturnServerValue", context.ToString());
String callbackspt = "function callServerData(arg,context) {" + callbackReference + ";}";
csm.RegisterClientScriptBlock(this.GetType(), "callServerData",callbackspt,true);
}
/// <summary>
/// 接受客戶端傳回的參數
/// </summary>
/// <param name="eventArgument"></param>
public void RaiseCallbackEvent(String eventArgument)
{
try
{
outputCount = eventArgument.ToString();
}
catch
{
outputCount = "output";
}
}
/// <summary>
/// Define method that returns callback result.(將運算好的結果返回)
/// </summary>
/// <returns></returns>
public string GetCallbackResult()
{
return outputCount;
}