使用 jQuery 的 ASP.NET 回发?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/751415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:43:25  来源:igfitidea点击:

ASP.NET postback with jQuery?

asp.netjqueryuser-interfacepostbackmodal-dialog

提问by mark smith

I have a ASP.NET button but recently, I replaced it with a standard HTML button ... What I need to do is a postback to an ASP.NET page and ensure a method is called.

我有一个 ASP.NET 按钮,但最近,我用一个标准的 HTML 按钮替换了它......我需要做的是回发到 ASP.NET 页面并确保调用一个方法。

The previous button was an ASP.NET button, so I had this event:

上一个按钮是一个 ASP.NET 按钮,所以我有这个事件:

Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
    UtilTMP.DisposeObjects()
    Server.Transfer("~\Forms\test.aspx", True)
End 

But I was using a button with a JavaScript ALERT and I recently changed to a jQuery UI Modal dialog but it doesn't wait for me to answer the question.. the postback happenes immediatly ... so I decided to change to a standard HTML button ... but I need to postback to the ASP.NET page and call a method like.

但是我使用了一个带有 JavaScript ALERT 的按钮,最近我改成了一个 jQuery UI 模态对话框,但它没有等我回答这个问题......回发立即发生......所以我决定改为标准 HTML按钮...但我需要回发到 ASP.NET 页面并调用类似的方法。

If I just postback it won't call the cleanup

如果我只是回发它不会调用清理

Protected Sub Cleanup()
    UtilTMP.DisposeObjects()
    Server.Transfer("~\Forms\test.aspx", True)
End 

采纳答案by Chris Brandsma

While you can do Postbacks with JQuery, it might be better to call a web method (web service that is in your page). The call could also be faster because you are not posting the entire page ViewState.

虽然您可以使用 JQuery 进行回发,但调用 Web 方法(页面中的 Web 服务)可能会更好。调用也可能更快,因为您没有发布整个页面 ViewState。

回答by David

See if this helps: http://www.codeproject.com/KB/aspnet/sample.aspx.

看看这是否有帮助:http: //www.codeproject.com/KB/aspnet/sample.aspx

Basically, you declare a dummy anchor tag:

基本上,您声明了一个虚拟锚标记:

<a id="anchorId" runat="server" onclick="return true" onserverclick="foo"></a> 

In your code behind, you need to declare a foo method:

在你后面的代码中,你需要声明一个 foo 方法:

protected void foo(object sender, EventArgs e)
{
    // Do something here
}

Then you can invoke this anchor's onclick function with this javascript:

然后你可以用这个 javascript 调用这个锚点的 onclick 函数:

document.getElementById('anchorId').click()

回答by David Basarab

Check out this article

看看这篇文章

http://www.deviantpoint.com/post/2009/03/12/Using-jQuery-UI-Dialogs-for-confirmation-windows.aspx

http://www.deviantpoint.com/post/2009/03/12/Using-jQuery-UI-Dialogs-for-confirmation-windows.aspx

The basic idea is you place the call back function in a hidden field and run an eval on the $(this).dialog('close');

基本思想是将回调函数放在隐藏字段中并在 $(this).dialog('close');

I used this to have it work in a Gridview, so if you want to know how to do that leave a comment.

我用它让它在 Gridview 中工作,所以如果你想知道如何做到这一点,请发表评论。

回答by tvanfosson

You need to set the __EVENTTARGET hidden field to an appropriate value if you want to trigger the event handler on postback. I would do it a different way, however. Put ASP buttons in your modal dialog that have the event handler associated with them. Have the click handler that pops up the dialog return false (so that the postback doesn't happen from that button click). This way your form is posted back from an ASP button and the handler, including the client-side hidden field setting, is invoked automatically.

如果要在回发时触发事件处理程序,则需要将 __EVENTTARGET 隐藏字段设置为适当的值。然而,我会用不同的方式来做。将 ASP 按钮放在您的模式对话框中,这些按钮具有与之关联的事件处理程序。让弹出对话框的单击处理程序返回 false(以便该按钮单击不会发生回发)。通过这种方式,您的表单从 ASP 按钮回发,并自动调用处理程序,包括客户端隐藏字段设置。

回答by Andrew Corkery

You should be able to stop the postback with whatever JS is attached to the button onclick event, regardless of whether it is a WebControl or a standar HTML button, by returning false.

无论是 WebControl 还是标准 HTML 按钮,您都应该能够通过返回 false 来使用附加到按钮 onclick 事件的任何 JS 来停止回发。

E.g. <input type="image" id="test" onclick="doWhatever();return false;" />

例如 <input type="image" id="test" onclick="doWhatever();return false;" />

回答by The Muffin Man

I came across this post through google so I suspect others will too. I tried the methods on here which didn't work for me. I will explain my solution, but first an overview of what I'm trying to do.

I have a form that uses jQuery ajax to post data to my webservice which in turn updates my database. I have an image upload form, for security reason you can't get the full file path to use in the ajax call to pass to the webservice... so it has to be done with a regular asp.net postback. So my dilemma is how to do both. What I did was use my ajax function to update the database, once I got a success back from the webservice I tell the form to do a postback to a certain method in the codebehind and then redirect to wherever. $("#Button1").click(function () { javascript: __doPostBack('ctl00$ContentPlaceHolder1$atest', '') });

我是通过谷歌看到这篇文章的,所以我怀疑其他人也会。我在这里尝试了对我不起作用的方法。我将解释我的解决方案,但首先概述我正在尝试做的事情。

我有一个表单,它使用 jQuery ajax 将数据发布到我的 web 服务,然后更新我的数据库。我有一个图像上传表单,出于安全原因,您无法获得在 ajax 调用中使用的完整文件路径以传递给 web 服务......所以它必须通过常规的 asp.net 回发来完成。所以我的困境是如何做到这两点。我所做的是使用我的 ajax 函数来更新数据库,一旦我从网络服务中获得成功,我就会告诉表单回发到代码隐藏中的某个方法,然后重定向到任何地方。 $("#Button1").click(function () { javascript: __doPostBack('ctl00$ContentPlaceHolder1$atest', '') });

This is essentially exactly the same as assigning a click function to an asp.net control, except instead of clicking the button I am telling javascript to execute it. atestwould be the ID i assigned to the asp.net control.
Note: the <%=myclientid.ClientID %>will not work here.

这与将单击功能分配给 asp.net 控件本质上完全相同,只是我不是单击按钮,而是告诉 javascript 执行它。 atest将是我分配给 asp.net 控件的 ID。
注意: 在<%=myclientid.ClientID %>这里不起作用。