异步刷新Browser的两种方法

1. 使用asp.net ajax

  1. <html xmlns="http://www.w3.org/1999/xhtml" >
  2. <head runat="server">
  3.     <title>title>
  4.     <script type="text/javascript">
  5.         setInterval("test()", 5);
  6.         function test() {
  7.             var obj = document.getElementById('Button1');
  8.             obj.click();
  9.         }
  10.     script>
  11. head>
  12. <body>
  13.     <form id="form1" runat="server">
  14.     <asp:ScriptManager ID="ScriptManager1" runat="server">
  15.     asp:ScriptManager>
  16.     <div>
  17.         <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  18.             <ContentTemplate>
  19.                 <asp:TextBox ID="TextBox1" runat="server">asp:TextBox>
  20.                 <div style="display:none;">
  21.                     <asp:Button ID="Button1" runat="server" Text="Refresh"/>
  22.                 div>
  23.             ContentTemplate>
  24.         asp:UpdatePanel>
  25.     div>
  26.     
  27.     form>
  28. body>
  29. html>

这里主要是用到了asp.net ajax 的UpdatePanel。 然后使用javascript定时自动的去点击按钮,模拟人工更新。

 

2. 通过webservice.htc 调用webservice来实现动态刷新

  1. <html xmlns="http://www.w3.org/1999/xhtml" >
  2. <head runat="server">
  3.     <title>title>
  4.     <script type="text/javascript">
  5.         setInterval("GetNewString()", 1000);
  6.         
  7.         function GetNewString() {
  8.             service.useService("WebServiceTest.asmx?WSDL", "WebServiceTest");
  9.             service.WebServiceTest.callService("GetTestString");
  10.         }
  11.         function Service_Result() {
  12.             if (event.result.error) {
  13.                 divFeatured.innerText = event.result.errorDetail.string;
  14.             }
  15.             else {
  16.                 divFeatured.innerText = event.result.value;
  17.             }
  18.         }
  19.     script>
  20. head>
  21. <body>
  22.     <form id="form1" runat="server">
  23.     <div>
  24.         <div id="service"  style="behavior:url(webservice.htc)"  onresult="Service_Result()">div>
  25.         <div id="divFeatured" style="width:100px; height:100px; background-color:Yellow; color:Red;">div>
  26.     div>
  27.     form>
  28. body>
  29. html>

 

  1. public class WebServiceTest : System.Web.Services.WebService
  2.     {
  3.         Random rd = new Random(DateTime.Now.Second);
  4.         [WebMethod]
  5.         public string GetTestString()
  6.         {
  7.             string[] strs = { "abc""12""3dfrw""34fg""ghjk23""56d4fg""df9g5""ett2r""34512" };
  8.             return strs[rd.Next(strs.Length-1)];
  9.         }
  10.     }

这里要去下载webservice.htc文件,并将其放在项目中。

你可能感兴趣的:(ASP.net,AJAX)