Skip to main content
 首页 » 编程设计

c#之如何在内容页面中使用ajax方法

2024年01月02日37bonelee

我在使用 Ajax 方法时遇到问题。我有一个母版页和一个内容页。我想在内容页中使用 Ajax 方法。为此

在页面加载的cs页面中:

Ajax.Utility.RegisterTypeForAjax(typeof(testcontent)); 

我已经添加了这个,因为我的内容页面名称是 testcontent。

这是我在cs页面中的方法:

 [Ajax.AjaxMethod()] 
  public int Getdatabyid(int id) 
  { 
        int count  = 0 ; 
       //getting the id and basing on that processing the data and assigning to count 
        return count; 
  } 

在我的 aspx 页面中:

function getdata(value) 
{ 
 var id = value; 
  var count = testcontent.Getdatabyid(id); 
} 

但我收到错误“测试内容不可用”。 我可以在从母版页继承属性的内容页中使用 Ajax 方法吗?在母版页中我放置了脚本管理器...请帮助我

请您参考如下方法:

我找到了问题的解决方案。

在 web.config 文件中,

添加这些代码行。

 <system.web> 
   <httpHandlers> 
     <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory,Ajax"/> 
   </httpHandlers> 
 </system.web> 

这将允许 Ajax 方法。