ASP.Net - 在服务器端触发按钮事件而不导致回发

发布于 2024-12-11 03:03:00 字数 621 浏览 4 评论 0 原文

我有一个问题,我过去也看到其他人也有过这个问题,但我找不到任何解决方案。

我想在页面上使用 ASP.Net 链接按钮(甚至是按钮),一旦单击,服务器端的事件代码就会触发,但我不希望它导致回发。

现在我知道这些控件旨在通过设计引起回发,并且您可以通过添加“return false;”来禁用此功能。到 Onclientclick 参数(或者 Attributes.Add("onclick", return false"); ),但请注意,这不是我要问的。我认为我的问题本质上是:我们如何调用服务器上的方法 -从客户端发送而不引起回发吗?(我可以感觉到问题本身存在一些逻辑问题,但其他几个人也遇到了同样的问题)

。 : http://ryanfarley.com/blog/archive/2005/03/11 /1886.aspx

解释了如何识别导致回发的控件。也许这有助于通过某种方式禁用回发效果但触发事件?如果是这样,如何避免页面刷新效果(我在Page_Load中没有任何内容,但页面仍然“刷新”或重新渲染。我不想要这个,可以避免吗?我没有已经找到办法了,

谢谢你的时间。

I have a question which I have seen other people have had in the past as well but I could not find any solutions to.

I want to use an ASP.Net Link Button (or even a Button) on a page which once clicked on, its event code on the serverside fires BUT I don't want it to cause a postback.

Now I know these controls are meant to cause postback by design and that you can disable this through adding: "return false;" to the Onclientclick parameter (or alternatively Attributes.Add("onclick", return false"); ) but please note that this is NOT what I am asking. I think my question essentially is: how can we call a method on the server-side from the client-side without causing a postback? Is this even possible? (I can sense some logical issues in the question itself but several other people have had the same problem).

And for you creative minds out there, I recently came across:
http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx

which explains how to identify the control which causes the postback. maybe this helps by somehow disabling the postback effects yet firing the event? if so, how can you avoid the page refresh effects (I don't have anything in the Page_Load, but the page still "refreshes" or re-renders. I don't want this, can this be avoided? I haven't figured out a way yet.

Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

栩栩如生 2024-12-18 03:03:00

要调用服务器端事件,您必须将数据发送到服务器,以便它知道要做什么。停止回发会停止发送数据,因此服务器永远不会知道客户端上发生了什么。

如果您只是想避免页面引用,那么最好的方法是使用 AJAX。在 ASP.NET Web 表单中,最简单的方法是将内容包装在 更新面板。另一种方法是研究将 PageMethods 与客户端脚本一起使用(本质上是调用来自客户端脚本的 Web 服务,然后充当服务器的代理)。

To call a server side event you HAVE to send data to the server so it knows what to do. Stopping the postback stops the data being sent, hence the server will never know about what has happened on the client.

If you simply want to avoid page refereshes then the way to go is to use AJAX. In ASP.NET web-forms the simplest way is to wrap stuff in an UpdatePanel. Another way is to look into using PageMethods with client-side script (essentially calling a web-service from client-side script that then acts as a proxy to the server).

趁微风不噪 2024-12-18 03:03:00

为什么不直接编写一个由按钮触发的 JavaScript 代码来进行您需要的客户端修改。如果您在活动期间需要来自服务器的任何动态数据,您可以使用 ajax 从服务器获取该数据。

Why not just code a javascript triggered by the button that does the clientside modifications you need. If you need any dynamic data from the server during the event you can fetch that with ajax from the server.

爱你不解释 2024-12-18 03:03:00

据我所知,这是不可能的。

As far as I know, that is not possible.

寂寞陪衬 2024-12-18 03:03:00

您可以通过将按钮放在 UpdatePanel 中来触发部分播放(因此页面不会重新加载)。

You could trigger a partial playback instead (so the page doesn't reload) by putting the button in an UpdatePanel.

自由如风 2024-12-18 03:03:00

你可以在这里做两件事。
- 使用更新面板并在 PageLoad 中添加 IsPostBack 的检查。
- 您可以做的另一件事是使用 jquery 进行 ajax 调用并指定在该按钮单击时执行的方法,以下链接应该可以帮助您。
http ://www.seoasp.net/post/2008/07/16/jQuery-To-Call-ASPNET-Page-Methods-and-Web-Services.aspx

you can do two things here.
- either use update panel and add a check in PageLoad for IsPostBack.
- The other thing you can do is that make a ajax call using jquery and specifying a method that would execute on that button click, following link should help you with that.
http://www.seoasp.net/post/2008/07/16/jQuery-To-Call-ASPNET-Page-Methods-and-Web-Services.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文