前些天,在练习使用 回调技术时, 发现回调总是发不回服务器上,仔仔细细排查了N遍,也没有发现错误之处,无奈之下,
大刀阔斧般的砍掉代码, 几乎减到了最简单,仍然还是不行,精神尽乎崩溃。
最后,只有一招,从网上下了一段代码, 运行,一切OK, 只好相互替换,最后竟然把全部代码都换了过来,可是,,只要是在我的文件中,就是不行, 天啊,,,,,,,,,,,,,,,,,,,,,,,,,
在几乎绝望的时候,发现两个页面文件名不一样,原来不行的那个文件中有中文,最后交换了文件名,终于查出原因!!!
不支持中文文件名!!!!!!!!!!
经过测试,发现只要是用了非ascii码的字符就会导致回调不成功
后来,把网站架在了 IIS7.0上,发现却没有这种问题了,可见问题出在了:
visual studio 2005/8的内置网页服务器 对含有非ascii码的字符的文件名支持不好!!!
附上完整代码(页面含中文名/不含时分别测试):
Test.aspx 页面部份:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
- <!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>
- </head>
- <script type="text/javascript">
- //主要回调的方法
- function GetMessageFromServer()
- {
- var arg= document.getElementById("TextBox1").value; //此处可以自己定义上传的参数
- var context= " ";
- <%-- 绑定的语法 --%>
- <%= ClientScript.GetCallbackEventReference(this, "arg", "JSCallback", "context")%> ;
- }
- //数据回到客户端时,调用的方法
- function JSCallback(result, context)
- {
- // 当回调结束后将执行这里的方法,把从服务器端获取的值进行客户端操作
- document.forms[0].TextBox1.value = result;
- }
- </script>
- <body>
- <form id="form1" runat="server">
- <div>
- <input type="button" id="Button1" value="回调演示" onclick="GetMessageFromServer()" />
- <br />
- <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
- </div>
- </form>
- </body>
- </html>
Test.axpx.cs的部份:
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- public partial class Test : System.Web.UI.Page, ICallbackEventHandler
- {
- string callbackResult;
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- //回传到服务器上时调用的方法
- public void RaiseCallbackEvent(string eventArgument)
- {
- // 服务器端操作
- callbackResult = "处理过的: " + eventArgument;
- }
- //发送到客户端去时调用的方法
- public string GetCallbackResult()
- {
- return callbackResult;
- }
- }