您的位置:首页 > 编程语言 > ASP

c# asp.net ajax 用法一例

2011-06-09 16:36 459 查看
c# asp.net ajax 用法一例

<%@ Page Language="C#" AutoEventWireup="true" ...%>

<script type="text/javascript">

//Ajax Asp.net 2.0方法

//---------------eg2 begin

function ReceiveServerData1(arg, context) {

document.form1.Text1.value = arg;

//document.getElementById('Text1').value=arg;

}

var tmp = 0;

function ReceiveServerData2(arg, context) {

Message1.innerText = arg;

tmp = arg;

}

function ProcessCallBackError(arg, context) {

}

//---------------eg2 end

</script>

<div>

输入:<input id="Text1" type="text" value="8" />

<input id="Button2" type="button" value="点击" onclick="CallTheServer1(Text1.value,null)"/>

</div>

<p></p>

<p></p>

<p>

演示:<span id="Message1">0       </span>

<input id="Button3" type="button" value="点击" onclick="CallTheServer2(tmp,null)" />

</p>

class Weather_WeatherComCn : System.Web.UI.Page, ICallbackEventHandler

{

protected void Page_Load(object sender, EventArgs e)

{

//Ajax Asp.net 2.0方法

//---------------------

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("'" + Page.UniqueID + "'", "arg", "ReceiveServerData1", "", "ProcessCallBackError", false);

String callbackScript1 = "function CallTheServer1(arg, context) {" +cbReference1 + "; }";

cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer1", 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);

//---------------------

if (!IsPostBack)

{

}

}

#region ICallbackEventHandler 成员

public int cbCount = 0;

public string GetCallbackResult()

{

//Response.Write(cbCount.ToString());

return cbCount.ToString();

}

public void RaiseCallbackEvent(String eventArgument)

{

cbCount = Convert.ToInt32(eventArgument) + 1;

}

#endregion

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: