问题描述
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><title>无标题页</title><scripttype="text/javascript">varxmlHttp;functioncreateXmlHttpRequest(){if(window.ActiveXObject){xmlHttp=newActiveXObject("Microsoft.XMLHTTP");}elseif(window.XMLHttpRequest){xmlHttp=newXMLHttpRequest();}}functiondostart(){createXmlHttpRequest();varurl="DynamicUpdate.aspx?task=reset";xmlHttp.open("GET",url,true);xmlHttp.onreadystatechange=startCallback;xmlHttp.send(null);}functionstartCallback(){if(xmlHttp.readyState==4){if(xmlHttp.status==200){setTimeout("pollServer()",5000);refreshTime();}}}functionpollServer(){createXmlHttpRequest();varurl="DynamicUpdate.aspx?task=foo";xmlHttp.open("GET",url,true);xmlHttp.onreadystatechange=pollCallback;xmlHttp.send(null);}functionpollCallback(){if(xmlHttp.readyState==4){if(xmlHttp.status==200){varmessage=xmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.data;if(message!="done"){varnew_row=createRow(message);vartable=document.getElementById("dynamicUpdateArea");vartable_body=table.getElementsByTagName("tbody").item(0);varfirst_row=table_body.getElementsByTagName("tr").item(1);table_body.insertBefore(new_row,first_row);setTimeout("pollServer()",5000);refreshTime();}}}}functioncreateRow(message){varrow=document.createElement("tr");varcell=document.createElement("td");varcell_data=document.createTextNode(message);cell.appendChild(cell_data);row.appendChild(cell);returnrow;}functionrefreshTime(){vartime_span=document.getElementById("time");vartime_val=time_span.innerHTML;varint_val=parseInt(time_val);int_val--;if(int_val>-1){setTimeout("refreshTime()",1000);time_span.innerHTML=int_val;}else{time_span.innerHTML=5;}}</script></head><body><h1>Ajax动态刷新实例</h1>页面将自动刷新自己:<inputtype="button"value="启动"id="go"onclick="dostart();"/><p>页面将在<spanid="time">5</span>秒后自动刷新</p><tableid="dynamicUpdateArea"style="text-align:left"><tbody><trid="row0"><td></td></tr></tbody></table></body></html>
DynamicUpdate.aspxusingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;publicpartialclassDynamicUpdate:System.Web.UI.Page{privateintcounter=1;protectedvoidPage_Load(objectsender,EventArgse){stringres="";stringtask=Request.Params["task"];stringmessage="";if(task=="reset"){counter=1;Session["counter"]=1;}else{counter=(int)Session["counter"];switch(counter){case1:message="Stevewalksonstage";break;case2:message="ipodsrock";break;case3:message="StevesaysMacsrule";break;case4:message="Changeiscoming";break;case5:message="Yes,OSXrunsonIntel-hasforyears";break;case6:message="MacwillsoonhaveIntelchips";break;case7:message="done";break;}counter++;Session["counter"]=counter;}res="<message>"+message+"</message>";Response.ContentType="text/xml";Response.Write(res);}}
运行时为什么总显示Stevewalksonstage
解决方案
解决方案二:
privateintcounter=1;
解决方案三:
奇怪的是在firefox中没有问题
解决方案四:
引用1楼MAOGE1987的回复:
privateintcounter=1;
我知道所以才放在session中