- 爱易网页
 
                        - 
                            AJAX教程
 
                        - 初学者AJAX简单例子一个 
 
                         
                    
                    
                    日期:2014-05-16  浏览次数:21003 次 
                    
                        
                         菜鸟求助AJAX简单例子一个
小弟想学学AJAX!!但是苦于代码整不明白!!!
求助AJAX简单例子一个,最好是c#的代码例子
PS:不要W3CSCHOOL的东西,弄不明白!!
跪谢给位大神!!!
谁的好用分全送上!!!
------解决方案--------------------
在开发之前我们得进行准备
1、下载ajaxpro.dll,并将其dll引用到项目中,此dll可以到http://www.okajax.com下载。
2、在webconfig的 <system.web>下加上
 <httpHandlers>
  <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro"/>
 </httpHandlers>
3、在Page_Load中添加
private void Page_Load(object sender, System.EventArgs e)
 {
  AjaxPro.Utility.RegisterTypeForAjax(typeof(WebApplication1.WebForm1));
 }
4、我们可以在服务器端的CS文件中写下函数,如:
[AjaxMethod]
public static string gettext(string str)
 {
  return str;
 }
但是要说明的是,每个ajax函数定义前必须加上[AjaxMethod]。
5、调用的时候就可以用以下方式进行
<script>
 function test()
 {
  alert(WebApplication1.WebForm1.gettext(‘ajax_Test’).value);
 }
 </script>
WebApplication1.WebForm1为当前窗体,gettext()是我们在cs文件中写的函数。
下面给出完整的代码:
aspx页面
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" Response.ContentType= "text/xml">
<HTML>
  <HEAD>
 <title>WebForm1</title>
 <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
 <meta name="CODE_LANGUAGE" Content="C#">
 <meta name="vs_defaultClientScript" content="JavaScript">
 <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 <script>
 function ss()
 {
  alert(WebApplication1.WebForm1.gettext(‘liutaotest’).value);
 }
 </script>
  </HEAD>
  <body MS_POSITIONING="GridLayout">
 <form id="Form1" method="post" runat="server">
  <FONT face="宋体"></FONT>
  <div id="div1">
  </div>
  <a href="javascript:" onclick=’ss()’>test</a>
 </form>
  </body>
</HTML>
cs文件:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using AjaxPro;
using System.Management;
namespace WebApplication1
{
  /// <summary>
  /// WebForm1 的摘要说明。
  /// </summary>
  public class WebForm1 : System.Web.UI.Page
  {
  
 private void Page_Load(object sender, System.EventArgs e)
 {
  AjaxPro.Utility.RegisterTypeForAjax(typeof(WebApplication1.WebForm1));
 }
 [AjaxMethod]
 public static string gettext(string sss)
 {
  string ss = sss;
   
  return ss;
 }
    
  
    
 #region Web 窗体设计器生成的代码
 override protected void OnInit(EventArgs e)
 {
  //
  // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
  //
  InitializeComponent();
  base.OnInit(e);
 }
    
 /// <summary>
 /// 设计器支持所需的方法 – 不要使用代码编辑器修改
 /// 此方法的内容。
 /// </summary>
 private void InitializeComponent()
 {   
  this.Load += new System.EventHandler(this.Page_Load);
 }
 #endregion