漂泊雪狼的博客

思考,讨论,分享C#,JavaScript,.NET,Oracle,SQL Server……技术
posts - 161, comments - 286, trackbacks - 9, articles - 10

导航

公告

ASP.NET 2.0中实现客户端回调的简化版

Posted on 2006-03-23 10:40 漂泊雪狼 阅读(...) 评论(...) 编辑 收藏

       上面总结的这么多,也许看起来有些晕,我下面给出一个简化版的实现无动态刷新的例子:
         可以在客户端无动态刷新的取服务器端的时间

新建一个页面:
后台代码:
public partial class Test : System.Web.UI.Page, ICallbackEventHandler
{
    public string strVal = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScriptManager cs = Page.ClientScript;
        string callback = cs.GetCallbackEventReference(this, "this.value", "FunTest", "this.value");
        txt.Attributes.Add("onclick", callback);
    }


    public void RaiseCallbackEvent(string eventArgument)
    {
        //char[] chars = eventArgument.ToCharArray();
        //Array.Reverse(chars);
        //strVal =  new string(chars);
        strVal =  DateTime.Now.ToString();
    }

    public string GetCallbackResult()
    {
        return strVal;
    }
}

页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script language="javascript">
        function FunTest(result,context)
        {
            document.getElementById("txt").value = result + "      " + context;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txt" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>