2010-04-26 51 views
1

我想知道如果RegisterTypeForAjax不能正常工作。我在下面的代码块结尾处看到错误。示例是从这里: http://www.ajaxtutorials.com/asp-net-ajax-quickstart/tutorial-introduction-to-ajax-in-asp-net-2-0-and-c/ASP.NET 2.0 C#AjaxPro RegisterTypeForAjax

任何想法,为什么我得到这个错误?谢谢。

ASP .NET 2.0 C#

这里是后台代码:

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.Collections; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 

using AjaxPro; 


namespace WebApplication1 
{ 
    public partial class Ajax_CSharp : System.Web.UI.Page 
    { 
     protected override void OnInit(EventArgs e) 
     { 
      base.OnInit(e); 
      Load += new EventHandler(Page_Load); 
     } 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      Utility.RegisterTypeForAjax(typeof(Ajax_CSharp)); 
     } 

     [ AjaxMethod(HttpSessionStateRequirement.ReadWrite) ] 
     public string GetData() 
     { 
      // method gets a row from the db and returns a string. 
     } 
} 

下面是ASPX页面:

<%@ Page Language="C#" AutoEventWireup="false" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Ajax_CSharp" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Untitled Page</title> 
    <script type="text/javascript"> 
     function GetData() 
     { 
      var response; 
      Ajax_CSharp.GetData(GetData_CallBack); 
     } 

     function GetData_CallBack(response) 
     { 
      var response = response.value; 

      if (response == "Empty") 
      { 
      alert("No Record Found."); 
      } 
      else if (response == "Error") 
      { 
      alert("An Error Occurred in Accessing the Database !!!"); 
      } 
      else 
      { 
      var arr = response.split("~"); 
      var empID = arr[0].split(","); 
      var empName = arr[1].split(","); 

      document.getElementById('dlistEmployee').length = 0; 

      for (var i = 0; i < empID.Length; i++) 
      { 
       var o = document.createElement("option"); 
       o.value = empID[i]; 
       o.text = empName[i]; 
       document.getElementById('dlistEmployee').add(o); 
      } 
      } 
     } 

     function dodisplay() 
     { 
      var selIndex = document.getElementById("dlistEmployee").selectedIndex; 
      var empName = document.getElementById("dlistEmployee").options(selIndex).text; 
      var empID = document.getElementById("dlistEmployee").options(selIndex).value; 

      document.getElementById("lblResult").innerHTML = "You have selected " + empName + " (ID: " + empID + ")"; 
     } 

    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <div style="text-align: center;"> 
     <input id="btnGetData" 
       onclick="GetData();" 
       type="button" 
       value="To Get Employee Data From DB" style="width: 203px" /> 
     &nbsp;&nbsp; 
     <asp:DropDownList id="dlistEmployee" OnTextChanged="dodisplay();" /> 
     <asp:Label id="lblResult" runat="server" Text="No Record Selected" /> 
    </div> 
    </form> 
</body> 
</html> 

运行它,然后点击按钮,我得到此错误:

网页错误详情

用户代理:Mozilla/4.0(compatible; MSIE 8.0; Windows NT 5.1;三叉戟/ 4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2; MS-RTC LM 8) 时间戳:星期一,2010年4月26日17时22分44秒UTC

消息: 'Ajax_CSharp' 是未定义 行:13 字符:11 代码:0 URI:http://localhost:4678/Default.aspx

回答

1

尝试使用全名。

在这种情况下的变化:

Ajax_CSharp.GetData(GetData_CallBack); 

WebApplication1.Ajax_CSharp.GetData(GetData_CallBack); 

正在发生的事情是,网页不知道Ajax_CSharp是什么。在使用AjaxPro时,您应始终使用类别的FullName,即Namespace.ClassName

+0

这解决了它添加处理程序!万分感谢! – Dan7el 2010-04-26 18:06:31

0

对于每个人仍然有这个问题。您需要在页面内添加一个<表单> html标记。没有必要包含任何全名标签,或者任何形式的输入。

<form id="Form1" method="post" runat="server"> 

验证,如果你已经在web.config

<httpHandlers> <add verb="POST,GET" path="ajaxPro/*.ashx" type="Ajax.PageHandlerFactory, AjaxPro" /> </httpHandlers>

+0

如果您正在使用Ajax.dll,请使用此处理程序< ' – 2015-08-18 18:42:26