时间:2021-07-01 10:21:17 帮助过:52人阅读
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void Render(HtmlTextWriter output)
{
string str;
while (true)
{//死循环保持长链接
str = "<script >window.parent.Change('" + DateTime.Now.ToLongTimeString() + "')</script>";
this.Context.Response.Write(str);
this.Context.Response.Flush();//输脚本调用出
System.Threading.Thread.Sleep(1000);
}
}
}
WebForm1.aspx
代码如下:
<!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 runat="server">
<title>Asp.net Server Push</title>
<script type="text/javascript">
function Change(str){
window.document.getElementById("div1").innerText=str;
}
function onload(){
var ifrpush = new ActiveXObject("htmlfile"); // 创建对象
ifrpush.open(); //打开
var ifrDiv = ifrpush.createElement("div"); //添加一个DIV
ifrpush.appendChild(ifrDiv); //添加到 htmlfile
ifrpush.parentWindow.Change=Change; //注册 javascript 方法 搞不明白为什么还要注册
ifrDiv.innerHTML = "<iframe src='Default.aspx'></iframe>"; //在div里添加 iframe
ifrpush.close(); //关闭
}
onload();
</script>
</head>
<body>
<div style=" float:left">现在时间是:</div>
<div id="div1"></div>
</body>
</html>
Default.aspx不需要做修改。
serverpush.rar