Asp.net 在visual studio 10中使用aspx.net的ajax

Asp.net 在visual studio 10中使用aspx.net的ajax,asp.net,ajax,Asp.net,Ajax,我正在写一个AJAX项目,我想知道正确的方法, (在VisualStudio的AJAX中不使用bulit) 我在JQuery中使用AJAX调用, 我的问题是: 在服务器端,我创建了AjaxProj.aspx, 不包含任何html代码, 在这个页面中,加载函数拥有捕获ajax请求的所有代码,并且在代码的末尾有 response.end(); 我真的很想知道我是否正确地使用了它(同样,我不想使用工具箱中的.NETAjax) ty all…请向我们粘贴一些代码以正确使用Ajax您应该使用webmet

我正在写一个AJAX项目,我想知道正确的方法, (在VisualStudio的AJAX中不使用bulit)

我在JQuery中使用AJAX调用, 我的问题是: 在服务器端,我创建了AjaxProj.aspx, 不包含任何html代码, 在这个页面中,加载函数拥有捕获ajax请求的所有代码,并且在代码的末尾有

response.end();
我真的很想知道我是否正确地使用了它(同样,我不想使用工具箱中的.NETAjax)


ty all…

请向我们粘贴一些代码以正确使用Ajax您应该使用webmethode函数,并且您的函数应该是静态的,例如:

 $(document).ready(function () {
  var text = $("#<%=ApplicationSearchResult.ClientID %>").val(); 
  // Add the page method call as an onclick handler for the div.
  $("#<%=ApplicationSearchResult.ClientID %>").autocomplete({
   source: function (request, response) {
   $.ajax({
   type: "POST",
   url: "Dashboard.aspx/GetTreeNodesByText",
  data: "{'text': '" + request.term + "'}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
 success: function (data) {
  response(data.d);
   }
  });
  },
  minLength: 1
  });//end auto complete

  });
$(文档).ready(函数(){
var text=$(“#”)val();
//将page方法调用添加为div的onclick处理程序。
$(“#”)自动完成({
来源:功能(请求、响应){
$.ajax({
类型:“POST”,
url:“Dashboard.aspx/GetTreeNodesByText”,
数据:“{'text':'”+request.term+“}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
答复(数据d);
}
});
},
最小长度:1
});//结束自动完成
});
服务器端功能:

[WebMethod]
public static List<string> GetTreeNodesByText(string text)
{


    List<string> Nodes = new List<string>();
    if (HttpContext.Current.Session["SerialSearchDT"] != null)
    {

        DataTable DT = HttpContext.Current.Session["SerialSearchDT"] as DataTable;
        DataRow[] filteredDataTable = DT.Select("NODE_NAME LIKE '" + text + "%'");
        //  DT.DefaultView.ToTable(true, "NODE_NAME");

        for (int i = 0; i < filteredDataTable.Length; i++)
        {
            string res = filteredDataTable[i][2].ToString();

            Nodes.Add(res);

        }//end For

    }//endIF
    return Nodes.Distinct().ToList();
}
<input type="button" value="click me" onclick="asyncServerCall(1);" />
[WebMethod]
公共静态列表GetTreneodesByText(字符串文本)
{
列表节点=新列表();
if(HttpContext.Current.Session[“SerialSearchDT”]!=null)
{
DataTable DT=HttpContext.Current.Session[“SerialSearchDT”]作为DataTable;
DataRow[]filteredatatable=DT.Select(“节点名称,如“'+text+“%”);
//DT.DefaultView.ToTable(true,“节点名称”);
for(int i=0;i

这是一个示例,我希望它能帮助您解决问题

首先编写服务器端代码:

[WebMethod()]
public static string GetData(int userid)
{
    /*do stuff*/
    return userId.ToString();
}
重要提示:该方法需要是静态的,并且必须具有WebMethod属性 然后是javascript部分:

function asyncServerCall(userid) {
    jQuery.ajax({
 url: 'WebForm1.aspx/GetData',
 type: "POST",
 data: "{'userid':" + userid + "}",
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 success: function (data) {
     alert(data.d);
 }

    });
}
然后别忘了传递js函数:

[WebMethod]
public static List<string> GetTreeNodesByText(string text)
{


    List<string> Nodes = new List<string>();
    if (HttpContext.Current.Session["SerialSearchDT"] != null)
    {

        DataTable DT = HttpContext.Current.Session["SerialSearchDT"] as DataTable;
        DataRow[] filteredDataTable = DT.Select("NODE_NAME LIKE '" + text + "%'");
        //  DT.DefaultView.ToTable(true, "NODE_NAME");

        for (int i = 0; i < filteredDataTable.Length; i++)
        {
            string res = filteredDataTable[i][2].ToString();

            Nodes.Add(res);

        }//end For

    }//endIF
    return Nodes.Distinct().ToList();
}
<input type="button" value="click me" onclick="asyncServerCall(1);" />


您必须在ScriptManager中启用PageMethods

是否使用webmethod?