仅用aspx文件实现Ajax调用后台cs程序。(实例)

仅用aspx文件实现Ajax调用后台cs无刷新程序。(实例)

两个文件:aaa.aspx 和aaa.aspx.cs

一、aaa.aspx 

 

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <script type="text/javascript">  
  2.        
  3.         function Request() {  
  4.             alert("1");  
  5.             $.ajax({  
  6.                 type: 'post',  
  7.                 url: 'Test442.aspx?method=Send2',   // 【调用后台的事件】  
  8.                 cache: false,  
  9.                 success: function (data) {  
  10.                     if (data == '00000') {  
  11.                         //receive();  
  12.                     }  
  13.                     var tbReceive = $('#tbReceive').val();  
  14.                     $('#tbReceive').val(tbReceive + data + ";");  
  15.                 },  
  16.                 error: function (XMLHttpRequest, textStatus) {  
  17.                 }  
  18.             });  
  19.         };  
  20.     </script>  


二、aaa.aspx.cs

 

 

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. protected void Page_Load(object sender, EventArgs e)  
    2.     {  
    3.         string result = "";  
    4.         if (!IsPostBack)  
    5.         {  
    6.   
    7.             if (Request["method"] != null)  
    8.             {  
    9.                 try  
    10.                 {  
    11.                     switch (Request.QueryString["method"])  
    12.                     {  
    13.                         case "Send2":     
    14.                              
    15.                             result = Send2();   <span style="font-family: Arial, Helvetica, sans-serif;">// 【执行的事件send2】</span>  
    16.   
    17.                             break;  
    18.                     }  
    19.                 }  
    20.                 catch (Exception ex)  
    21.                 {  
    22.                     result = ex.Message;  
    23.                     LogManager.WriteLog(ex.Message);  
    24.                 }  
    25.                 Response.Clear();  
    26.                 Response.ContentType = "text/plain";  
    27.                 Response.ContentEncoding = System.Text.Encoding.UTF8;  
    28.                 Response.Write(result);  
    29.                 Response.End();  
    30.             }  
    31.   
    32.         }  
    33.     }  
posted @ 2016-12-07 13:45  fightingyy  阅读(139)  评论(0编辑  收藏  举报