如何使用带有asp.net的jQuery来回发onclientclick

时间:2021-10-19 04:09:35

I want to recreate the the update panel postback without using an update panel to do the postback. What is the generic method for doing this?

我想重新创建更新面板回发而不使用更新面板来进行回发。这样做的通用方法是什么?

For example, on *, when you vote up or down on a question it does a postback to update the database and I would bet they didn't use an update panel.

例如,在*上,当您在问题上向上或向下投票时,它会进行回发以更新数据库,我敢打赌他们没有使用更新面板。

What do I have?

我有什么?

I have a table with table data. When I click on the td item as a whole column, I want to do an update to the database and also update a gridview on the page itself. The gridview shows all the currently clicked items in the table because it was updated via "our method".

我有一个表数据表。当我单击td项作为整列时,我想对数据库进行更新,并在页面本身上更新gridview。 gridview显示表中当前所有点击的项目,因为它是通过“我们的方法”更新的。

Looking for a good generic method I could use for a lot of async postbacks without update panel.

寻找一个很好的通用方法,我可以用于很多没有更新面板的异步回发。

3 个解决方案

#1


17  

The way that Stack Overflow works differs in two important ways from that CodeProject article.

Stack Overflow的工作方式与CodeProject文章的两个重要方式不同。

  • Stack Overflow is making its AJAX request against an ASP.NET MVC controller action, not a standalone ASPX page. You might consider this as the MVC analogue of an ASP.NET AJAX page method. In both cases, the ASPX method will lag behind in terms of performance.

    Stack Overflow正在针对ASP.NET MVC控制器操作发出AJAX请求,而不是独立的ASPX页面。您可能会将此视为ASP.NET AJAX页面方法的MVC模拟。在这两种情况下,ASPX方法在性能方面都会落后。

  • Stack Overflow's AJAX request returns a JSON serialized result, not arbitrary plaintext or HTML. This makes handling it on the client side more standardized and generally cleaner.

    Stack Overflow的AJAX请求返回JSON序列化结果,而不是任意明文或HTML。这使得在客户端处理它更加标准化并且通常更清洁。

For example: when I voted this question up an XmlHttpRequest request was made to /questions/171000/vote, with a "voteTypeId" of 2 in the POST data.

例如:当我投票给这个问题时,XmlHttpRequest请求被发送到/ questions / 171000 / vote,POST数据中的“voteTypeId”为2。

The controller that handled the request added my vote to a table somewhere and then responded with this JSON:

处理请求的控制器将我的投票添加到某个表的某个表,然后使用此JSON进行响应:

{"Success":true,"NewScore":1,"Message":"","LastVoteTypeId":2}

Using that information, this JavaScript takes care of updating the client-side display:

使用该信息,此JavaScript负责更新客户端显示:

var voteResult = function(jClicked, postId, data) {
  if (data.Success) {
    jClicked.parent().find("span.vote-count-post").text(data.NewScore);
    if (data.Message)
      showFadingNotification(jClicked, data.Message);
  }
  else {
    showNotification(jClicked, data.Message);
    reset(jClicked, jClicked);

    if (data.LastVoteTypeId) {
      selectPreviousVote(jClicked, data.LastVoteTypeId);
    }
  }
};

If you're using WebForms, the example of calling page methods that you found on my blog is definitely in the right ballpark.

如果您正在使用WebForms,那么您在我的博客上找到的调用页面方法的示例肯定是正确的。

However, I would suggest that you consider a web service for any centralized functionality (like this voting example), instead of page methods. Page methods seem to be slightly easier to write, but they also have some reuse drawbacks and tend to provide an illusion of added security that isn't really there.

但是,我建议您考虑使用任何集中功能(如此投票示例)的Web服务,而不是页面方法。页面方法似乎稍微容易编写,但它们也有一些重用缺点,并且往往会提供一种额外的安全性错觉,而实际上并不存在。

This is an example of doing the same thing you found, but with web services (the comments on this post actually led to the post you found):

这是一个做你发现的相同事情的例子,但是有了web服务(这篇文章的评论实际上导致了你发现的帖子):

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

#2


2  

You can just use a standard AJAX call to accomplish this. Create a .aspx page which updates the database in its Page_Load method, and displays any desired information (like the current DB value after the update) as XML. Then make an AJAX call to that page using jQuery.

您可以使用标准的AJAX调用来完成此任务。创建一个.aspx页面,该页面在其Page_Load方法中更新数据库,并以XML格式显示任何所需信息(如更新后的当前DB值)。然后使用jQuery对该页面进行AJAX调用。

You can also return an HTML fragment (i.e. an updated GridView), and use jQuery to insert the updated HTML into the current page.

您还可以返回HTML片段(即更新的GridView),并使用jQuery将更新的HTML插入当前页面。

Edit: Sample 2 on this page should be very close to what you want:
http://www.codeproject.com/KB/ajax/AjaxJQuerySample.aspx

编辑:此页面上的示例2应该非常接近您想要的内容:http://www.codeproject.com/KB/ajax/AjaxJQuerySample.aspx

#3


2  

This link is what I found to be the best thing to do and allow me to use javascript and web methods.

这个链接是我发现的最好的事情,并允许我使用JavaScript和Web方法。

#1


17  

The way that Stack Overflow works differs in two important ways from that CodeProject article.

Stack Overflow的工作方式与CodeProject文章的两个重要方式不同。

  • Stack Overflow is making its AJAX request against an ASP.NET MVC controller action, not a standalone ASPX page. You might consider this as the MVC analogue of an ASP.NET AJAX page method. In both cases, the ASPX method will lag behind in terms of performance.

    Stack Overflow正在针对ASP.NET MVC控制器操作发出AJAX请求,而不是独立的ASPX页面。您可能会将此视为ASP.NET AJAX页面方法的MVC模拟。在这两种情况下,ASPX方法在性能方面都会落后。

  • Stack Overflow's AJAX request returns a JSON serialized result, not arbitrary plaintext or HTML. This makes handling it on the client side more standardized and generally cleaner.

    Stack Overflow的AJAX请求返回JSON序列化结果,而不是任意明文或HTML。这使得在客户端处理它更加标准化并且通常更清洁。

For example: when I voted this question up an XmlHttpRequest request was made to /questions/171000/vote, with a "voteTypeId" of 2 in the POST data.

例如:当我投票给这个问题时,XmlHttpRequest请求被发送到/ questions / 171000 / vote,POST数据中的“voteTypeId”为2。

The controller that handled the request added my vote to a table somewhere and then responded with this JSON:

处理请求的控制器将我的投票添加到某个表的某个表,然后使用此JSON进行响应:

{"Success":true,"NewScore":1,"Message":"","LastVoteTypeId":2}

Using that information, this JavaScript takes care of updating the client-side display:

使用该信息,此JavaScript负责更新客户端显示:

var voteResult = function(jClicked, postId, data) {
  if (data.Success) {
    jClicked.parent().find("span.vote-count-post").text(data.NewScore);
    if (data.Message)
      showFadingNotification(jClicked, data.Message);
  }
  else {
    showNotification(jClicked, data.Message);
    reset(jClicked, jClicked);

    if (data.LastVoteTypeId) {
      selectPreviousVote(jClicked, data.LastVoteTypeId);
    }
  }
};

If you're using WebForms, the example of calling page methods that you found on my blog is definitely in the right ballpark.

如果您正在使用WebForms,那么您在我的博客上找到的调用页面方法的示例肯定是正确的。

However, I would suggest that you consider a web service for any centralized functionality (like this voting example), instead of page methods. Page methods seem to be slightly easier to write, but they also have some reuse drawbacks and tend to provide an illusion of added security that isn't really there.

但是,我建议您考虑使用任何集中功能(如此投票示例)的Web服务,而不是页面方法。页面方法似乎稍微容易编写,但它们也有一些重用缺点,并且往往会提供一种额外的安全性错觉,而实际上并不存在。

This is an example of doing the same thing you found, but with web services (the comments on this post actually led to the post you found):

这是一个做你发现的相同事情的例子,但是有了web服务(这篇文章的评论实际上导致了你发现的帖子):

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

#2


2  

You can just use a standard AJAX call to accomplish this. Create a .aspx page which updates the database in its Page_Load method, and displays any desired information (like the current DB value after the update) as XML. Then make an AJAX call to that page using jQuery.

您可以使用标准的AJAX调用来完成此任务。创建一个.aspx页面,该页面在其Page_Load方法中更新数据库,并以XML格式显示任何所需信息(如更新后的当前DB值)。然后使用jQuery对该页面进行AJAX调用。

You can also return an HTML fragment (i.e. an updated GridView), and use jQuery to insert the updated HTML into the current page.

您还可以返回HTML片段(即更新的GridView),并使用jQuery将更新的HTML插入当前页面。

Edit: Sample 2 on this page should be very close to what you want:
http://www.codeproject.com/KB/ajax/AjaxJQuerySample.aspx

编辑:此页面上的示例2应该非常接近您想要的内容:http://www.codeproject.com/KB/ajax/AjaxJQuerySample.aspx

#3


2  

This link is what I found to be the best thing to do and allow me to use javascript and web methods.

这个链接是我发现的最好的事情,并允许我使用JavaScript和Web方法。