【发布时间】:2013-12-30 10:07:33
【问题描述】:
在我的 asp.net 应用程序中,我使用了异步 javascript 回调。 要求是一旦用户输入邮政编码,就应该调用异步 javascript 回调函数。并且服务器端代码必须返回与邮政编码相关的城市。
我做的编码是:
<asp:TextBox ID="txtPostCode" runat="server" CssClass="textbox" onchange="javascript: getCityAndStreet();" BorderStyle="Ridge" Height="20px" style="margin-left: 10px" Width="442px"></asp:TextBox>
这个文本框onchange 它将调用javascript function getCityAndStreet()
function getCityAndStreet()
{
var postalCode = document.getElementById('<%=txtPostCode.ClientID%>');
CallServer(postalCode.value, "");
}
function ReceiveCityAndStreet(rValue,context)
{
alert(rValue);
var city = document.getElementById('<%= txtCity.ClientID%>');
city.value = rValue;
}
这里的 CallServer 是服务器端运行时 javascript,注册如下
protected void Page_Init(object sender, EventArgs e)
{
String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveCityAndStreet", "context",true);
String callbackScript;
callbackScript = "function CallServer(arg, context)" + "{ " + "alert('Entered inside CallServer' + arg);" + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
}
我实现了ICallBackHandler,通过这个我得到了两种方法:
string ICallbackEventHandler.GetCallbackResult()
{
return cityAndStreet;
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
txtStreet.Enabled = true;
txtCity.Enabled = true;
cityAndStreet = eventArgument;
}
在 page_load 函数中,我刚刚禁用了 txtStreet.Enabled。 txtCity.Enabled 文本框和引发回调事件我正在启用它。
这里的问题是RaiseCallBackEvent 不起作用。我的意思是它没有被隐式触发。
(此应用程序不能直接在通过 SharePoint 站点访问的浏览器中访问)
【问题讨论】:
-
获取异步调用结果相当复杂,当你有很多调用时变得更加复杂,尝试使用jQuery ajax
标签: javascript asp.net ajax sharepoint callback