所谓的ajax动态无刷新技术---WebForm_DoCallback
时间:2010-10-15 来源:北京Net老牛队
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gg.aspx.cs" Inherits="gg" %>
<!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>无标题页</title>
<script type="text/javascript">
var tmp=0;
function ReceiveServerData2(arg, context)
{
Message1.innerText = arg;
tmp=arg;
}
function ProcessCallBackError(arg, context)
{
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
输入:<input id="Text1" type="text" value="8" />
<input id="Button1" type="button" value="点击" onclick="CallTheServer1(Text1.value,null)"/>
</div>
<p></p>
<p></p>
<p>
演示:<span id="Message1">0 </span>
<input id="Button2" type="button" value="点击" onclick="CallTheServer2(tmp,null)" /></p>
</form>
</body>
</html>
后台
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Text;
public partial class gg : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
private int cbCount;
protected void Page_Load(object sender, EventArgs e)
{
// function CallTheServer1(arg, context)
// {
// WebForm_DoCallback('__Page',arg,ReceiveServerData1,
// function ReceiveServerData1(arg, context)
// {
// Message1.innerText =arg;value1 = arg;
// },
// null,false);
// }
ClientScriptManager cs = Page.ClientScript;
StringBuilder context1 = new StringBuilder();
context1.Append("function ReceiveServerData1(arg, context)");
context1.Append("{");
context1.Append("document.getElementById('Text1').value=arg;");
context1.Append("");
context1.Append("}");
String cbReference1 = cs.GetCallbackEventReference(this, "arg",
"ReceiveServerData1", context1.ToString());
String callbackScript1 = "function CallTheServer1(arg, context) {" +
cbReference1 + "; }";
cs.RegisterClientScriptBlock(this.GetType(), "avp", callbackScript1, true);
//function CallTheServer2(arg, context)
// {WebForm_DoCallback('__Page',arg,ReceiveServerData2,\"\",ProcessCallBackError,false);
String cbReference2 = cs.GetCallbackEventReference("'" +
Page.UniqueID + "'", "arg", "ReceiveServerData2", "",
"ProcessCallBackError", false);
String callbackScript2 = "function CallTheServer2(arg, context) {" +
cbReference2 + "; }";
cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer2", callbackScript2, true);
}
public void RaiseCallbackEvent(String eventArgument)
{
cbCount = Convert.ToInt32(eventArgument) + 1;
}
public string GetCallbackResult()
{
return cbCount.ToString();
}
}
执行大致流程:button-------click------>CallTheServer1(text,obj)------------->CallTheServer1(arg, context)
------------->RaiseCallbackEvent()----------->GetCallbackResult()--------------------->ReceiveServerData1(text,obj)*可以放在html中*---->结束