源文件:
web:
<title>WebForm4</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script> function Test(objSrc,objDes) 
{
try {

xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
xmlHttp = new XMLHttpRequest();
}
        
xmlHttp.onreadystatechange = function()
 {

if (xmlHttp.readyState == 4)
 {
if (xmlHttp.status == 200)
 {
var data = xmlHttp.responseText;
document.getElementById(objDes).value = data;
  }
  }
}
        
xmlHttp.open("GET","WebForm6.aspx?testid=" + objSrc.value);
xmlHttp.send(null);
}
function Test1(owner, item, evt)
{
var id = document.getElementById("TextBoxJCode1").value;
AjaxMethod.wjytest(id,test2);
}
function test2( str )
{
var vid = document.getElementById("TextBoxJName1").value;
vid.value = str.value;
} </script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">

<asp:TextBox id="TextBoxJName1" style="Z-INDEX: 101; LEFT: 392px; POSITION: absolute; TOP: 272px"
runat="server" Width="136px" Height="40px"></asp:TextBox>
<asp:TextBox id="TextBoxJCode1" style="Z-INDEX: 102; LEFT: 400px; POSITION: absolute; TOP: 336px"
runat="server" Width="128px" Height="40px" Wrap="False"></asp:TextBox><INPUT id="aa" style="Z-INDEX: 103; LEFT: 216px; WIDTH: 136px; POSITION: absolute; TOP: 336px; HEIGHT: 32px"
type="button" value="Button" onclick="Test1()"></FONT></form>
</body>
</HTML>
调用的cs文件:
using System;namespace WebApplication2
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
public Class1()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
public static string wjytest(string id)
{
string name = "";
if ( id == "1")
name = "这是第一个名称";
else if ( id == "2")
 name = "这是第二个名称"; return name; }
}
}
webconfig配置:
    
    <httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>可是在web中总是提示找不到AjaxMethod?
我在web中注册吧--Ajax.Utility.RegisterTypeForAjax(typeof(WebApplication2.这儿总是找不到AjaxMethod));怎么回事?我在项目中也引用了ajax控件

解决方案 »

  1.   

    Ajax.Utility.RegisterTypeForAjax(typeof(WebApplication2.Class1);
      

  2.   

    Ajax.Utility.RegisterTypeForAjax(typeof(WebApplication2.Class1));
      

  3.   

    namespace WebApplication1
    {
    /// <summary>
    /// _Default 的摘要说明。
    /// </summary>
    public class _Default : System.Web.UI.Page
    {
    protected System.Web.UI.HtmlControls.HtmlTableCell tdFooter;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面 Ajax.Utility.RegisterTypeForAjax(typeof(_Default));
    } #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 [Ajax.AjaxMethodAttribute(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public string CheckLogin(string strUserID, string strPassword)
    {
    string strReturn = "";
    string strUserName = "超级管理员"; //检查用户名称和登录密码是否为空
    if ((strUserID.Trim() == "") || (strPassword.Trim() == ""))
    return "用户名称或者登录密码不能为空白。";
    //检查是否为超级管理员
    if (strUserID == "Admin")
    if (strPassword != "superman"){strReturn += "超级管理员密码错误。";} if (strReturn == "")
    {
    HttpContext.Current.Session["UserID"] = strUserID;
    HttpContext.Current.Session["UserName"] = strUserName;
    } return strReturn;
    }

    [Ajax.AjaxMethodAttribute()]
    public string GetPassword(string strUserID)
    {
    string strReturn = "";

    if ((strUserID.Trim() != "") && (strUserID.Trim() != "Admin"))
    {
    strReturn += "test";
    }

    return strReturn;
    }
    }
    }
      

  4.   

    调用:<script>
    function ServerCheckLogin() {
    var ret = _Default.CheckLogin(document.Form1.txtUserName.value, document.Form1.txtPassword.value).value;

    if (ret == "") {
    document.location.href = "Main.htm";
    }else{
    document.getElementById("tdMessage").innerText = ret;
    document.Form1.txtUserName.focus();
    document.Form1.txtUserName.select();
    }
    }
    </script>
      

  5.   

    还是不行
    同样的问题
    我的是直接调用的cs类,不是aspx后台对应的哪个类文件
    真头疼
      

  6.   

    你可以WebForm4后台文件中添加:Ajax.Utility.RegisterTypeForAjax(typeof(WebApplication2.Class1));  --要引用的类名
    在客户端:
    Class1.wjytest(id,test2);这样就行啦