且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用jQuery的ajax请求部分替代asp.net页面?

更新时间:2023-02-11 14:38:12

首先,看样品code你贴,你打算如何进行Ajax调用,您将需要启用 PageMethods 在你的页面。 关于如何做到这一点本教程一部分。

Firstly, looking at the sample code you posted and how you plan to make the Ajax call, you will need to enable PageMethods in your page. See this tutorial on how to do that part.

实际的Ajax调用使用jQuery可以很简单,比如这个:

The actual ajax call using jQuery can be as simple as this:

$('select').change(function () {
     $.post('Default.aspx/MethodName',{selectedVal:$(this).val()}, function(data){
         //as far as placing the result in the input text field, I think this will do:
         $(this).parents('tr').find('input:text').val(data);
     });
});

现在,你的方法名方法必须是这样的:

Now, your MethodName method would have to look like this:

  [WebMethod]
  public static string MethodName(string selectedVal)
  {
     return "something";
  }

的jsfiddle(减去AJAX调用)。

对不起,我误解了问题,OP要更换整排。这怎么办呢:

Sorry, I misread the question, the OP wants to replace the WHOLE row. This will do then:

 $(this).parents('tr').replaceWith('<tr><td colspan="3"><div>'+data+'</div></td></tr>');

全方面:

$('select').change(function () {
         $.post('Default.aspx/MethodName',{selectedVal:$(this).val()}, function(data){
             //as far as replacing the row, this will do:
               $(this).parents('tr').replaceWith('<tr><td colspan="3"><div>'+$(this).val()+'</div></td></tr>');
         });
    });