我需要从客户端调用 ASP.Net 服务器端代码。因为我在 ascx 用户控件中,所以不能使用 [webmethod] + PageMethods,因为这仅适用于 ASPX 页面。
我认为可行的下一个选项是手动创建客户端回调。使用此方法,客户端将成功调用服务器代码,但客户端不会得到回调。这是我设置客户端脚本块的代码:
protected void Page_Load(object sender, EventArgs e)
{
String strJSCallbackPrefix = this.ClientID;
ClientScriptManager cm = Page.ClientScript;
String cbReference = cm.GetCallbackEventReference(this, "arg", strJSCallbackPrefix + "ReceiveServerData", "context");
String callbackScript = "function " + strJSCallbackPrefix + "CallServer(arg, context){" + cbReference + "; }";
cm.RegisterClientScriptBlock(this.GetType(), strJSCallbackPrefix + "CallServer", callbackScript, true);
String strReceiveServerData = "function " + strJSCallbackPrefix + "ReceiveServerData(arg, context){document.getElementById('btnCancel').value='thisandthat';}";
cm.RegisterClientScriptBlock(this.GetType(), strJSCallbackPrefix + "ReceiveServerData", strReceiveServerData, true);
Button2.OnClientClick = strJSCallbackPrefix + "CallServer('test message',1); " + "return false;";
}
public string GetCallbackResult()
{
return returnValue;
}
public void RaiseCallbackEvent(string eventArgument)
{
returnValue = "11223" + eventArgument;
}
有谁知道为什么客户回电没有被解雇?