ASP.NET postback with jQuery?

坚强是说给别人听的谎言 提交于 2019-11-28 18:51:45
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.

David

See if this helps: 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:

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

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

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

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.

Check out this article

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');

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

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.

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

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', '') });

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. atest would be the ID i assigned to the asp.net control.
Note: the <%=myclientid.ClientID %> will not work here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!