上面总结的这么多,也许看起来有些晕,我下面给出一个简化版的实现无动态刷新的例子:
可以在客户端无动态刷新的取服务器端的时间
新建一个页面:
后台代码:
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>